vast25 wrote:
in this code e is EventArgs so it will also not work if my app in background.
This is a different story. But, first of all, you should not "know" if a key is hit; you need to handle events. Solution 1 and 2 did not explained it to you and are just wrong, written out of context.
Now, as your application may be in the background (in the sense you mean), that is, with no activated window (it may have no windows at all or none of them are activated), you can register a
hot key globally for the whole system. This is how it is done:
RegisterHotKey function (Windows)[
^],
UnregisterHotKey function (Windows)[
^].
To do it for .NET, you need to use P/Invoke. Everything is already done for you here:
http://www.pinvoke.net/default.aspx/user32.registerhotkey[
^],
http://www.pinvoke.net/default.aspx/user32.unregisterhotkey[
^].
You can get more flexibility if you set up a global Windows Hook, but this is much harder. For a global hook, you would need to set up the hook in a native DLL, not your .NET assembly. Hook code is quite hard to debug, there is a lot of danger to destabilize your system for a while, and so on. Anyway, read about it:
Hooks (Windows)[
^],
Hooks Overview (Windows)[
^],
SetWindowsHookEx function (Windows)[
^].
—SA