Hotkey (not global) in Windows Forms .NET

You can override ProcessCmdKey and handle your hotkeys there, either in a control or a form.

From MSDN:

The ProcessCmdKey method first determines whether the control has a ContextMenu, and if so, enables the ContextMenu to process the command key. If the command key is not a menu shortcut and the control has a parent, the key is passed to the parent's ProcessCmdKey method. The net effect is that command keys are "bubbled" up the control hierarchy. In addition to the key the user pressed, the key data also indicates which, if any, modifier keys were pressed at the same time as the key. Modifier keys include the SHIFT, CTRL, and ALT keys.

For example:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    // if it is a hotkey, return true; otherwise, return false
    switch (keyData)
    {
        case Keys.Control | Keys.C:
            // do something
            return true;
        default:
            break;
    }

    return base.ProcessCmdKey(ref msg, keyData);
}

You can override a form's ProcessCmdKey() method, it runs before any control on the form will see the key stroke. If you really need this to be at the application level (all forms) then you should have your main form implement the IMessageFilter interface. For example:

public partial class Form1 : Form, IMessageFilter {
    public Form1() {
        InitializeComponent();
        Application.AddMessageFilter(this);
        this.FormClosed += (o, e) => Application.RemoveMessageFilter(this);
    }
    public bool PreFilterMessage(ref Message m) {
        // Catch WM_KEYDOWN message
        if (m.Msg == 0x100 && (Keys)m.WParam == Keys.F1) {
            MessageBox.Show("Help me!");
            return true;
        }
        return false;
    }
}

If you have just one form. You can set the Form.KeyPreview=true and then add code to the form's key event.


Comments

  1. Clayton

    • 2019/8/26

    Please note that this code will not trigger events in console application projects. You have to use WinForms project for events to fire.

  2. Ameer

    • 2020/12/14

    Hotkey (not global) in Windows Forms .NET . Ask Question Asked 11 years, 3 months ago. Active 10 years, 1 month ago. Viewed 4k times 12 3. In my Windows Forms

  3. Noah

    • 2019/6/25

    Learn how to create Global Hotkeys properly in a C# desktop saying that no two programs can have the same global hotkey combination.

  4. Rayan

    • 2015/2/1

    Download source file and demo projects - 597.3 KB; Download DLLs - 171.3 KB; Download demo - 64.5 KB; Introduction. Sometimes, your form or window (depending on if you're building a WinForm or a WPF application) may need to respond to a hotkey or shortcut (used interchangeably in this article) regardless of if the app is focused or running in the background.

  5. Keanu

    • 2020/4/20

    NET implement global shortcuts and making the keys changeable. if not all of the functionalities of the WinForm equivalent and though it 

  6. Chaim

    • 2017/4/26

    Using the Code [Version 2] The Simple Way. If you are developing a Windows Forms application and prefer drag & drop programming, there is a component named GlobalEventProvider inside the assembly Gma.UserActivityMonitor.dll. Just drag and drop it to your form and create events using the property editor events tab.

  7. Jacoby

    • 2018/1/12

    There are multiple ways to add HotKeys in your WinForms application is not the correct way to proceed when working with Global HotKeys.

  8. Giuliani

    • 2016/7/21

    Once you have called Hotkey.RegisterHotkey from your code, the hotkey is registered, and will trigger the code in the Hotkey.handleHotKeyEvent, every time the user presses that specific hotkey. Note Hotkeys with special keys such as F1-F12, space and similar requires a slightly different approach. The methods are essentially the same, but you will need to retrieve the "virtual key code" of the special key, and use it as a parameter in the Hotkey.registerHotkey method instead of the Asc

  9. Leland

    • 2016/12/27

    Is it possible to assign a global hotkey to an application that is http://social.msdn.microsoft.com/forums/en-us/clr/thread/BB49A3B0- 

  10. Crosby

    • 2015/3/7

    Add Smart Hot key control to Toolbox (if not available), then drag and drop on to your form. Open your form code file and add the following line of code at the constructor of the form. C#. Copy Code. this .hotKey1.HotKeyPressed += new SmartHotKey.HotKey.HotKeyEventHandler (hotKey1_HotKeyPressed); Add the hot key press event handler. C#. Copy Code.

  11. De Luca

    • 2016/7/16

    If you are looking for a way to set a global hotkey for your C# I'm not that much of a internet reader to be honest but your sites 

  12. Waylon

    • 2017/9/14

    Public Class MyMainForm Inherits System.Windows.Forms.Form Implements IMessageFilter Private WM_KEYDOWN As Integer = &H100 Private WM_KEYUP As Integer = &H101 Public Function PreFilterMessage(ByRef m As Message) As Boolean Dim keyCode As Keys = CType(CInt(m.WParam), Keys) And Keys.KeyCode If m.Msg = WM_KEYDOWN And keyCode = Keys.Escape Then Console.WriteLine("Ignoring Escape")

  13. Kyree

    • 2017/11/6

    NET library that allows registering system-wide hotkeys that can be used to trigger A managed library to handle global hotkeys in Windows Forms and WPF 

  14. Hysi

    • 2021/6/10

    HotKeyControl does not treat Windows keys as modifiers, but rather as Net implements global keyboard shortcuts and makes keys variable.

  15. Luka

    • 2017/12/15

    Sometimes, your form may need to respond to a hotkey even when it is not focused. There is no built in .NET functionality that allows for 

  16. Rizzo

    • 2017/4/28

    abc: A small C# (.NET) Library which allows binding of global HotKeys to any Application's Windows (including Windows Apps such as explorer.exe), 

Comments are closed.

Recent Posts