I've built a winforms application which checks for CTR+ALT+S and CTRL+ALT+E keypresses, using by overriding the ProcessCmdKey method. This works great, but if the screensaver goes on and then goes off the form doesn't have focus and the keypresses aren't intercepted. How can I receive these even if the form does not have focus?
+5
A:
I'm aware of two methods:
RegisterHotKey() - You can use the RegisterHotKey() function to define a system-wide hot key. If the user presses the hotkey, Windows sends a WM_HOTKEY message.
Win32 Hooks - This is an old API originally designed to support computer-based training (CBT) applications, but I believe that Windows still supports it. The API allows you to intercept and possibly modify messages, mouse actions, and keystrokes for any window.
These are Win32 APIs, not .NET APIs, but .NET uses the same underlying components of Windows so the methods ought to work with .NET.
Peter Neubauer
2009-02-16 18:35:08
The old hooks were designed for a lot more - they're basically pre/post intercept routines for all windows. However, there is a hook dedicated for CBT which intercepts everything.
gbjbaanb
2009-02-16 18:47:19
+7
A:
Alexander Werner has a "System Hotkey Component" project over at Code Project that wraps the RegisterHotkey() API into a user control that's really easy to implement.
Adam L
2009-02-16 18:49:30
Checked out the system hotkey component. Perfect example. I did a but if refactoring to get it how I liked it, but it works great. Thanks.
Jeremy
2009-02-17 22:07:53