Search code examples
c#console-applicationglobal-hotkey

Multiple global hotkeys in C# console application


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


Solution

  • 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.