Register more than one hotkey with RegisterHotKey

前端 未结 1 524
情深已故
情深已故 2020-12-10 16:45

I found this little piece of code to register an hotkey:

    [DllImport(\"user32.dll\")]
    public static extern bool RegisterHotKey(IntPtr hWnd, int id, in         


        
相关标签:
1条回答
  • 2020-12-10 17:21
     RegisterHotKey(this.Handle, this.GetType().GetHashCode(), 1, (int)'A')
    

    Don't use GetHashCode() here. Just number your hot keys, start at 0. There isn't any danger of getting the ids mixed up, hot key ids are specific for each Handle. You'll get the id back in the WndProc() method. Use m.WParam.ToInt32() to get the value:

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x0312) {    // Trap WM_HOTKEY
            int id = m.WParam.ToInt32();
            MessageBox.Show(string.Format("Hotkey #{0} pressed", id));
        }
        base.WndProc(ref m);
    }
    
    0 讨论(0)
提交回复
热议问题