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?
3 Answers
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.

- 1,121
- 1
- 8
- 13
-
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 Feb 17 '09 at 22:07
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.

- 847
- 5
- 5
-
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 Feb 16 '09 at 18:47
I know this question was asked a long time ago, but if anyone comes here, it might be the solution :
Just set the KeyPreview
property (of your form) to True
. This way your form will handle KeyPress
event before any other control, even if one of them has the focus
I've had quite a similar issue. Check here : KeyPress at form level with controls
Hope it helps

- 121
- 10