How to make any action on the ESC key if the program is minimized?

    1 answer 1

    Need to use WinApi function.

    1. Importing:

      [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);` 
    2. Add a field to the class that will store the link to the hokey

       const int MYACTION_HOTKEY_ID = 1; 
    3. We register hot key

       // Modifier keys codes: Alt = 1, Ctrl = 2, Shift = 4, Win = 8 // Compute the addition of each combination of the keys you want to be pressed // ALT+CTRL = 1 + 2 = 3 , CTRL+SHIFT = 2 + 4 = 6... RegisterHotKey(this.Handle, MYACTION_HOTKEY_ID, 6, (int) Keys.F12); 
    4. We catch the pressed buttons:

       protected override void WndProc(ref Message m) { if (m.Msg == 0x0312 && m.WParam.ToInt32() == MYACTION_HOTKEY_ID) { // My hotkey has been typed // Do what you want here // ... } base.WndProc(ref m); } 

    Took from here:

    https://stackoverflow.com/questions/15413172/capture-a-keyboard-keypress-in-the-background

    • Answer-link - this is bad, pnyatnenko? - andreycha
    • one
      @andreycha, well, I will consider. - iluxa1810 September