0

I would like to be able to allow users of my application to press Ctrl+Shift+numeric keypad keys to act as a navigational shortcut in an application I am developing. To my surprise it appears to be impossible to recognise this key combination.

Pressing Shift+keypad activates the extended functions displayed on most keyboards, such as cursor keys (on 2, 4, 6 and 8), page down/up (on 3 and 9), end/home (on 1 and 7), etc.

The various key events in WinForms report these exactly as if they were the actual cursor keys etc. being pressed. I'm therefore unable to distinguish between e.g. Shift+KeyPad4 and CursorLeft. I can't simply look for Ctrl+CursorLeft instead of Ctrl+Shift+KeyPad4, as people may be using Ctrl+CursorLeft for text editing purposes.

Is there any way I can properly detect these shifted numeric keypad keyboard combinations?

Thanks!

Adam Dawes
  • 188
  • 2
  • 10

1 Answers1

1

Use a class to keep track of what keys are pressed. When you receive an OnKeyDown event, store that key as down and then when you come to When you receive an OnKeyUp event, remove that key from the store.

Then on the same OnKeyDown event, after storing the key press, do a check to see if all three keys that you expect are down.

It would look something like so:

public void OnKeyDown(EventArgs ...) {
    _keyStore.KeyDown(eventArgs.keyPress);

    if(_keyStore.AltCtrlKeyPad4IsDown()) { //Abstract this so you can perform multiple checks.
        //Do shortcut.
    }

}

public void OnKeyUp(EventArgs ...) {
    _keyStore.KeyUp(eventArgs.keyPress);
}

Note that this is sudo code and will not compile.

Kieran Devlin
  • 1,373
  • 1
  • 12
  • 28
  • 1
    This could be even cooler if you used bit flags for a couple of things. For example, your modifiers could be one set of bit flags and the key would be another enumerator. This would save you from having to write a huge amount of methods for everything. Definitely like the overall format of this answer, though. – milespossing Jul 15 '19 at 15:42
  • Thanks for the suggestion, unfortunately I couldn't find a solution along these lines. I monitored the Shift and Ctrl keys and checked these when pressing (e.g.) "4" in the numeric keypad. The result was as follows: I press and hold Shift: _shift flag is set. I press and hold Ctrl: _ctrl flag is set. I press numpad 4: three key events fire in sequence: Shift is released, CursorLeft is pressed, Shift is pressed again. I still therefore can't distinguish between the user pressing Ctrl+Shift+numpad4, and between pressing Ctrl+CursorLeft. Very frustrating... – Adam Dawes Jul 16 '19 at 08:01
  • Please state the key combination you want to use to act as a shortcut to an event. – Kieran Devlin Jul 16 '19 at 08:30
  • As per my original post, I want to trap Ctrl+Shift+numeric-keypad keys. The combination of Shift+numeric-keypad is returning their alternative function keys (e.g., cursor left/right/up/down) rather than any combination that reflects what has actually been physically pressed on the keyboard. I can't therefore distinguish between (e.g.) Ctrl+Shift+numeric-keypad-4 and Ctrl+CursorLeft. – Adam Dawes Jul 16 '19 at 12:38
  • It sounds like you want to bypass the run time abstraction and implement a low level hook to capture the input. If you get the same input from that then it will be a restriction at the hardware level. (i.e the keyboard) https://stackoverflow.com/questions/604410/global-keyboard-capture-in-c-sharp-application – Kieran Devlin Jul 16 '19 at 12:53
  • I tried the keyboard hook you linked to, and it did the same thing. Instead of Ctrl pressed, Shift pressed, NumPad4 pressed, I still end up getting Ctrl pressed, Shift pressed, Shift released, CursorLeft pressed. Perhaps it is the keyboard sending these messages to begin with as you suggest, rather than Windows interpreting them. How frustrating. Thanks for your assistance anyway! – Adam Dawes Jul 17 '19 at 11:55