简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM