The most trivial way to determine keystrokes for the case when your application is not a background one and is active:
public class Program { public static void Main() { ConsoleKeyInfo key; do { key = Console.ReadKey(); Console.WriteLine(key.Key + " клавиша была нажата"); } while (key.Key != ConsoleKey.Escape); // по нажатию на Escape завершаем цикл } }
If you need a keystroke tracking method when the application is not active and is a background process, then we need to import the user32.dll helper library user32.dll :
[DllImport("user32.dll")] public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc); [DllImport("user32.dll")] public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
There is a good answer regarding this option: Capture a keyboard keypress in the background . In principle, if you use the search, then the answers can be found. Or here's a ready, good and complete answer: Background Key Press Listener