Interception of keystrokes, even if the form has no focus

I created a winforms application that checks for pressing CTR + ALT + S and CTRL + ALT + E using an override of the ProcessCmdKey method. This works fine, but if the splash screen continues and then leaves, the form has no focus and keystrokes are not intercepted. How can I get them even if the form has no focus?

+4
source share
3 answers

Alexander Werner has a System Hotkey Component "on the Code project, which wraps the RegisterHotkey () API in a user control that is really easy to implement.

+7
source

I know two methods:

  • RegisterHotKey () - You can use the RegisterHotKey () function to define a system-wide hotkey. If the user presses a hot key, Windows sends a WM_HOTKEY message.

  • Win32 Hooks is an old API originally designed to support computer technology (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.

This is a Win32 API, not a .NET API, but .NET uses the same basic Windows components, so the methods should work with .NET.

+5
source

I know this question was asked a long time ago, but if anyone comes here, it could be a solution:

Just set the KeyPreview property (of your form) to True . This way your form will handle the KeyPress event in front of any other control, even if one of them has focus

I had a pretty similar problem. Check here: Form-level KeyPress with controls

Hope this helps

0
source

All Articles