I'd like to know how to make my application catch any keydown/keyup EVEN when the application form is minimized. Say, the program is minimized and then user press 'A', a popup control will appear. Note that my program works but not when minimized. Thanks.
-
you want to catch the "A" when your app is focused or you want a global shortcut that will "activate" your app? – xanatos Feb 28 '15 at 18:54
-
hi, thank you so much for replying, what I want is to make my form focused all the time even when it is minimized. Cause when I run my program, and I clicked on anywhere outside the program form, of course it doesn't work. It's like the WordWeb software (a dictionary software) that even such software is minimized to the try and when you press CTRL+ALT+W, the software will pop up. Something like that. – Glenn Posadas Feb 28 '15 at 18:59
-
1See http://stackoverflow.com/a/11378213/613130 . You put that code in your MainWindow and it works. – xanatos Feb 28 '15 at 19:08
-
hello xanatos, please see my reply to the answer below. pls. Thank you so much for the link, but again, see my reply. – Glenn Posadas Mar 01 '15 at 16:24
1 Answers
What you require is HotKeys. There is no built in .NET functionality that allows for this, but it is built into the Win32 API.
So you need two methods RegisterHotKey
and UnregisterHotKey
, which is in user32.dll
and System.Runtime.InteropServices
namespace which contains the important DllImportAttribute
we will need to call the above two Win32 API function to register the Hotkey.
Here are two links which contains a good demo to use hotkeys.
1) Global Hotkeys: Register a hotkey that is triggered even when form isn't focused,
2) Simple steps to enable Hotkey and ShortcutInput user control
You have to use these modifiers and Virtual-Key Codes (symbolic constant names, hexadecimal values, and mouse or keyboard equivalents for the virtual-key codes used by the system.):
MOD_ALT (0x0001)
MOD_CONTROL (0x0002)
MOD_NOREPEAT (0x4000)
MOD_SHIFT (0x0004)
MOD_WIN (0x0008)
for example A is 0x41.

- 7,896
- 2
- 29
- 44
-
hi, I already marked your answer as 'answer'. However, the codes in your first given link is not working without CTRL. the "[In] uint fsModifiers," is necessary, whenever I try to remove it, it gives me an error, BREAK OR CONTINUE, blabla bla. What I really want is to make my program catch/detect the NUMLOCK button even when minimized. I use this code to tell if numlock is activated or not: if (Console.NumberLock== true) – Glenn Posadas Mar 01 '15 at 16:24
-
-
I could not understand your problem clearly. How about the second link? – Utsav Dawn Mar 01 '15 at 18:19
-
@xanatos - ok thanks for that. I'll try your answer later tonight. For sir Ustave, the first and the second link does the same, right? Ok, so here's the concept of my small project. When my program is minimized, it will still detect if the user press the NUMLOCK, and then the program will produce a pop-up message saying the current state of the numlock (on or off?). That's it! In the future, I will make my program to work even if it is in the tray. The code (Console.NumberLock == true) works when the form is being focused only. – Glenn Posadas Mar 02 '15 at 06:41
-
1ok, mark as solved :) Thanks Xanatos for your comment. I made my fsModifiers = 0; :) :) :) Cool!!! – Glenn Posadas Mar 04 '15 at 03:48