I have a application that runs in the background. I have to generate some event whenever a user press F12 at anytime. So what I need that to capture a key-press. In my application, if any time a user press F10 some event will be performed. I don't understand how to do that?
Have anyone any idea how to do that?
N:B: It is a winforms application. It doesn't need to have focus my form. My main window may remain in system tray but still it have to capture the keypress.
What you want is a global hotkey.
Import needed libraries at the top of your class:
Add a field in your class that will be a reference for the hotkey in your code:
Register the hotkey (in the constructor of your Windows Form for instance):
Handle the typed keys by adding the following method in your class:
In case you have problem running Otiel's solution:
You need to include:
Another doubt for newbies like me: "top of the class" really means top of your class like this (not namespace or constructor):
You don't need to add user32.dll as reference to the project. WinForms always load this dll automatically.