I am setting up global shortcut keys in 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
The code to process this is:
[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 in XP or Windows Server 2003 it does not.
Any ideas where it goes wrong?
Looking at the documentation for RegisterHotKey it states that the MOD_NOREPEAT flag is not supported on Vista/XP/2K. I suspect that this is your problem.
You should check the return value which would tell you immediately that something is wrong.