1

To register global hotkey in console application in C#, I am using the code posted here: https://stackoverflow.com/a/3654821/3179989

It works perfectly, but when I add multiple hotkeys, pressing one hotkey will result in running all of the hotkey_pressed events:

   HotKeyManager.RegisterHotKey(Keys.E, KeyModifiers.Control);
   HotKeyManager.HotKeyPressed += new EventHandler<HotKeyEventArgs>(HotKeyManager_HotKeyPressed);

   HotKeyManager.RegisterHotKey(Keys.A, KeyModifiers.Control);
   HotKeyManager.HotKeyPressed += new EventHandler<HotKeyEventArgs>(HotKeyManager_HotKeyPressed2);

Can somebody help me to change the code, or suggest me any other solution/idea for multiple global hotkeys in c# console.

thanks in advance

Community
  • 1
  • 1
NESHOM
  • 899
  • 16
  • 46
  • 1
    This will not work with the class you are using. Only register one HotKeyPressed-Event. In this event you can check the HotKeyEventArgs with a simple if-statement to determine which of your hotkeys was pressed. – user1567896 Jul 06 '14 at 16:04
  • @user1567896: thanks for your comment. You mean like this? if (e.Modifiers.ToString() + " " + e.Key.ToString()=="Control E") {\\dosomething} { – NESHOM Jul 06 '14 at 16:18
  • 1
    You don't need to convert the key to string. You can do something like `e.Key == Keys.Enter` and then check ´KeyModifiers´ if the ´control key´ is pressed as well. – user1567896 Jul 06 '14 at 16:28
  • @user1567896 Thank you, How can make this the answer? – NESHOM Jul 06 '14 at 17:14
  • I have posted my comment as an answer below, so you can accept it ;). – user1567896 Jul 06 '14 at 17:19

1 Answers1

1

This will not work with the class you are using.

Only register one HotKeyPressed-Event. In this event you can check the HotKeyEventArgs with a simple if-statement to determine which of your hotkeys was pressed.

user1567896
  • 2,398
  • 2
  • 26
  • 43