I'm working on a little tray application that runs an action when a numpad key is pressed while the Numlock is off - with the following conditions:
- The application listens to the key presses even when it's out of focus.
- The app MUST distinguish between numpad vs. non-numpad keys that have the same effect (e.g. Numpad7 acts like Home when Numlock is off vs. Dedicated Home button on the keyboard).
I used this answer to register my global hotkeys so that the app listens to the keypresses while out-of-focus. The app runs but both Numpad7 and Home return the same keycode.
I also looked at this answer which gave me some insight that m.LParam.ToInt32() >> 24 == 1
implies Numpad key press, which didn't work for all keys (only seems to work for Enter/Return).
I have found this excellent Javascript-run website that actually checks the location of the keypress, returning Numpad when any numpad key is pressed with Numlock both on or off.
Any idea how to get a similar Numpad Keypress check in C#?
UPDATE:
Experimenting with what @Jimi suggested on the comments below, I have noticed that when checking against m.MSG == WM_KEYDOWN
, evaluating m.LParam >> 24
results in 0
for non-Numpad keys, and 1
for Numpad keys. However, my application is checking against WM_HOTKEY
, in order to listen to keypresses when the application is out of focus.
Here's how my WNdProc
method looks like:
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
// check if we got a hot key pressed.
if (m.Msg == 0x0312) // check if WM_HOTKEY
{
// get the keys.
Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF);
ModifierKeys modifier = (ModifierKeys)((int)m.LParam & 0xFFFF);
// here m.LParam is identical for both numpad-home and non-numpad home
bool isNumpad = ((int)m.LParam >> 24) == 0;
// invoke the event to notify the parent.
if (KeyPressed != null)
KeyPressed(this, new KeyPressedEventArgs(modifier, key, isNumpad));
}
}
There isn't any obvious way in the documentation of WM_HOTKEY
to check if the hotkey pressed is part of extended keyboard.