RegisterHotkey only works in WIndows 7, not XP, server 2003

I set global keyboard shortcuts on Windows using the RegisterHotKey method

public static int MOD_CONTROL = 0x2;
public static int WM_HOTKEY = 0x312;

RegisterHotKey(this.Handle, 0, MOD_CONTROL | MOD_NOREPEAT, 96); 
// ctrl numpad0

Code for processing:

[DllImport("user32.dll")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);            

 protected override void WndProc(ref Message m)
    {

        if (m.Msg == WM_HOTKEY)         
        {
            MessageBox.Show("a hotkey is pressed"); //this also only shows in win7

            if (m.WParam.ToInt32() == 0) //ctrl numpad0
            {
                MessageBox.Show("Hotkey ctrl numpad0 pressed"); 
                // works fine in win7

            }

        }
        base.WndProc(ref m);
    }

On my Windows 7 PC, this works, but on XP or Windows Server 2003 it is not. Any ideas where this is going wrong?

+3
source share
1 answer

Looking at the documentation for RegisterHotKey , she states that the MOD_NOREPEAT flag is not supported in Vista / XP / 2K. I suspect this is your problem.

You should check the return value, which immediately tells you that something is wrong.

+3
source

All Articles