Register more than one hotkey with RegisterHotKey

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

[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 == 0x0312) MessageBox.Show("Hotkey pressed"); base.WndProc(ref m); } public FormMain() { InitializeComponent(); //Alt + A RegisterHotKey(this.Handle, this.GetType().GetHashCode(), 1, (int)'A'); } 

It works fine, but my problem is that I want to use two different shortcuts. I know that the second parameter is id, so I suppose I can create another identifier and add a new if statement in the WndProc function, but I'm not sure how I would do it.

In short, how do I go about creating a second shortcut?

Thanks,

+7
source share
1 answer
  RegisterHotKey(this.Handle, this.GetType().GetHashCode(), 1, (int)'A') 

Do not use GetHashCode () here. Just type your hot keys, start from scratch. There is no danger of identifier infection; hotkey identifiers are specific to each pen. You will return the identifier 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); } 
+15
source

All Articles