Why is my Registered Hotkey not triggered when pressing the hotkey combination

474 Views Asked by At

I've implemented the user32.dll register and unregister hot key methods, but after registering a hotkey, I never get the WndProc message 0x0312 when pressing the hotkey. Can someone review my code and help me understand why I never get the 0x0312 message.

The combination of hotkeys I've tried so far:

  • Ctrl + Shift + F12
  • F12
  • F9

My implementation is just the most common implementation:

[DllImport("c:\\windows\\system32\\user32.dll")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
[DllImport("c:\\windows\\system32\\user32.dll")]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
protected override void WndProc(ref Message m) {
    if(m.Msg == 0x0312) {
        int id = m.WParam.ToInt32();
        switch(id) {
            case 0:
                MessageBox.Show("Ctrl + Shift + F12 HotKey Pressed ! Do something here ... ");
                break;
        }
    }
}

I created a singleton class to handle the registration and unregistration of hotkeys:

public class HotKeyHandler {

    //Hotkey register and unregister.
    [DllImport("c:\\windows\\system32\\user32.dll")]
    public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
    [DllImport("c:\\windows\\system32\\user32.dll")]
    public static extern bool UnregisterHotKey(IntPtr hWnd, int id);

    public const int MOD_ALT = 0x0001;
    public const int MOD_CONTROL = 0x0002;
    public const int MOD_SHIFT = 0x0004;
    public const int MOD_WIN = 0x0008;

    byte ID = 0;

    /// <summary>
    /// Keep the constructor private due to singleton implementation
    /// </summary>
    private HotKeyHandler() { }
    public static HotKeyHandler Instance = new HotKeyHandler();

    public bool RegisterHotKey(IntPtr handle, int modifier, Key key) {
        bool returnVal = RegisterHotKey(handle, ID, modifier, (int) key);
        ID++;
        return returnVal;
    }

    public void UnregisterAllHotKeys(IntPtr handle) {
        for(short s = 0; s <= ID; s++) {
            UnregisterHotKey(handle, s);
        }
    }
}

Finally I register the HotKey like this:

HotKeyHandler.Instance.RegisterHotKey(this.Handle, HotKeyHandler.MOD_CONTROL | HotKeyHandler.MOD_SHIFT, Key.F12);
1

There are 1 best solutions below

0
Simon Jensen On

I'd like to take the time to answer my own question just in case others might find themselves in the same.. rather irritating situation..

So after some digging and prodding I finally found out what was the problem, I was looking at the value for the Key ID that was passed to the RegisterHotKey method and noticed that the value I got did not match with the actual ID for the Key.
Turns out there exists two types of Key enums, there is the System.Windows.Input.Keyand the System.Windows.Forms.Keys. I was not aware of this and was using Input.Key which has different values from Forms.Keys

TL:DR
Use Forms.Keys for RegisterHotKey() and not Input.Key