24

Possible Duplicate:
How can I register a global hot key to say CTRL+SHIFT+(LETTER) using WPF and .NET 3.5?

I'd like to have multiple global hotkeys in my new app (to control the app from anywhere in windows), and all of the given sources/solutions I found on the web seem to provide with a sort of a limping solution (either solutions only for one g.hotkey, or solutions that while running create annoying mouse delays on the screen).

Does anyone here know of a resource that can help me achive this, that I can learn from? Anything?

Thanks ! :)

Community
  • 1
  • 1
  • For a WPF solution you can look at my answer at: [enter link description here][1] [1]: http://stackoverflow.com/questions/48935/how-can-i-register-a-global-hot-key-to-say-ctrlshiftletter-using-wpf-and-ne/9330358#9330358 – Eric Ouellet Feb 17 '12 at 15:03
  • Is this what you want? http://stackoverflow.com/questions/48935/c-using-wpf-and-net-35-how-can-i-register-a-global-hot-key-to-say-ctrlshiftlett – Gishu Sep 17 '08 at 08:36

3 Answers3

40

The nicest solution I've found is http://bloggablea.wordpress.com/2007/05/01/global-hotkeys-with-net/

Hotkey hk = new Hotkey();

hk.KeyCode = Keys.1;
hk.Windows = true;
hk.Pressed += delegate { Console.WriteLine("Windows+1 pressed!"); };

hk.Register(myForm); 

Note how you can set different lambdas to different hotkeys

Ohad Schneider
  • 36,600
  • 15
  • 168
  • 198
  • Is it possible to get this class to work with the mouse buttons (Keys.LButton and Keys.RButton)? I have tried using the same class and can only get it to work with Keyboard keys. – Bobby Byrnes Apr 18 '17 at 18:20
7

http://www.codeproject.com/KB/cs/CSLLKeyboardHook.aspx

If you're not using .net 3.5.

arul
  • 13,998
  • 1
  • 57
  • 77
  • 2
    -1 keyboard hooks are not a good choice for global hotkeys. – CodesInChaos Mar 12 '12 at 15:44
  • 2
    Nothing else conforms to the specification: "to control the app from anywhere in windows", so either provide better insight [sic] or remove your comment, please. – arul Jun 14 '13 at 21:58
  • [`RegisterHotKey`](http://msdn.microsoft.com/en-us/library/windows/desktop/ms646309.aspx) works no matter which window has focus, so it conforms to spec. – CodesInChaos Jun 14 '13 at 22:00
  • Not in the presence of elevated windows, just scroll down to the comments would you? Not to mention that the same hotkey maybe overriden by currently active window, making your 'oh so' global hotkey useless. – arul Jun 14 '13 at 22:05
  • Unfortuntely, although it works, it cancels the key input from other apps. So registering 'a' would mean other apps wouldn't be able to use 'a'. – Dan W Mar 25 '15 at 19:24
  • @DanW: Yes, you need to re-emit that event. Possibly send the key message yourself. – arul Mar 25 '15 at 19:43
  • Aha, I simply used `e.Handled = false;` instead of true in the event and it worked. Now I just need to handle cases such as Ctrl+[whatever-key] or Shift+[any-key]. Any ideas? Shift works, so does say, 'Y', but I can't register both together. – Dan W Mar 25 '15 at 19:58
  • GetKeyState / GetAsyncKeyState should give you the information. – arul Mar 25 '15 at 20:00
  • I'm lost sorry. Win32 stuff I presume, so out of reach for me, and I wouldn't know how to convert from KeyCode/KeyValue to it or what code to add etc. – Dan W Mar 25 '15 at 20:22
5

I would handle this by using P/Invoke to call RegisterHotKey() for each hotkey, and then use NativeForm (assuming you are using WinForms) to be notified of the WM_HOTKEY message. This should keep most of your hotkey code in one place.

Andy
  • 30,088
  • 6
  • 78
  • 89