Lt. Cmdr. Data
Lt. Cmdr. Data

Reputation: 3

C# MouseKeyHook: Certain Key + Modifier Combinations Not Working

I've been working with the MouseKeyHook NuGet package, and it's working great for capturing most input. But I'm having some problems catching certain combinations of keys + modifiers.

public static class InputHandler
{
    private static IKeyboardMouseEvents _GlobalHook;
    public static IKeyboardMouseEvents GlobalHook => _GlobalHook;

    public static void Subscribe()
    {
        _GlobalHook = Hook.AppEvents();
        _GlobalHook.KeyDown += KeyDown;
    }

    private static void KeyDown(object sender, KeyEventArgs e)
    {
        Console.WriteLine("Output: " + e.Modifiers + " + " + e.KeyCode);
    }
}


Lets try hitting a few keys and see what the output is:

Key: A
> Output: None + A

Key: Shift & A
> Output: Shift + A

Key: Shift & Control & Alt & A
> Output: Shift, Control, Alt + A

Fantastic! Exactly what you'd expect. Now what about the number bar at the top of the keyboard?

Key: 1
> Output: None + D1

Key: Shift & Control & Alt & 3
> Output: Shift, Control, Alt + D3

Okay, again, exactly what you'd expect. No problems... What about the 0 key?

Key: 0
> Output: None + D0

Key: Shift & 0
> Output: Shift + D0

Key: Shift & Control & 0
> Output: Shift, Control + ShiftKey    <---- What????

Key: Shift & Control & Alt & 0
> Output: Shift, Control, Alt + D0


So what's going on here? Why does the event not fire properly when exactly D0 + Control + Shift is pressed? Also worth noting is that this is a KeyDown event, so the output repeats as long as you have the keys held down, but when that "ShiftKey" output is printed it never repeats, which is... odd.

Worst case, I can always switch up my bindings, but I've noticed this oddity over a number of different keys + modifier combinations (mostly oem keys, the numpad, and numbar), so it would be nice to know why this happens.

Upvotes: 0

Views: 751

Answers (1)

Robyn
Robyn

Reputation: 1374

I can think of two possibilities:

It could be an operating system keyboard shortcut.

https://support.microsoft.com/en-au/help/967893/input-method-editor-keyboard-shortcut-ctrl-shift-0-switches-the-input

Many keyboards are not physically capable of correctly detecting every possible combination of keys. The exact details vary depending on how the keyboard's circuit board is laid out. Shift-A or Ctrl-X will always work, holding down every key at once only works on the fanciest of keyboards, in between there's a grey area.

https://en.wikipedia.org/wiki/Rollover_(key)#Key_jamming_and_ghosting

Upvotes: 0

Related Questions