1

Как сделать какое либо действие на клавишу ESC если программа свернута?

1 ответ 1

1

Нужно использовать WinApi функции.

  1. Импортируем либы:

    [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. Добавляем поле в класс, которое будет хранить ссылку на хотекй

    const int MYACTION_HOTKEY_ID = 1;
    
  3. Регистрируем хоткей

    // 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. Ловим нажатые кнопки:

    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);
    }
    

Взял отсюда:

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

2
  • Ответ-ссылка -- это плохо, пнятненько?
    – andreycha
    4 сен 2016 в 15:29
  • 1
    @andreycha, хорошо, учту.
    – iluxa1810
    4 сен 2016 в 15:43

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.