How to distinguish between multiple key combinatio

2019-09-18 02:56发布

问题:

I have a problem with key combinations. I would simulate all keyboard funciton but i can't distinguish between one modifier+key and 2 modifiers+key.

For example if i press shift+ key it work but if i press shift+altGR+ key it doesn't works becouse my program detect only altGR even if i checked if shift is pressed.

if(Keyboard.IsKeyDown(Key.RightAlt))
{
    if(Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))    
    {
        Key key = e.Key;
        MessageBox.Show("Shift+AlgGr+key");

    }
    else
    {
        Key key = e.Key;
        MessageBox.Show("AlgGr+key");

    }
}

How can i solve it? Thank you for every idea.

回答1:

    var sb = new StringBuilder();
    var modifierKeys = Keyboard.PrimaryDevice.Modifiers;

    if (modifierKeys.HasFlag(ModifierKeys.Alt))
        sb.Append("ALT ");
    if (modifierKeys.HasFlag(ModifierKeys.Control))
        sb.Append("CTRL ");
    if (modifierKeys.HasFlag(ModifierKeys.Shift))
        sb.Append("SHIFT ");

    Debug.Print(sb.ToString());

This should be what you need.

You can check for specific keys by using:

    var sb = new StringBuilder();

    if (Keyboard.PrimaryDevice.IsKeyDown(Key.RightAlt))
        sb.Append("RIGHT ALT ");
    if (Keyboard.PrimaryDevice.IsKeyDown(Key.LeftAlt))
        sb.Append("LEFT ALT ");
    if (Keyboard.PrimaryDevice.IsKeyDown(Key.LeftShift))
        sb.Append("LEFT SHIFT ");
    if (Keyboard.PrimaryDevice.IsKeyDown(Key.RightShift))
        sb.Append("RIGHT SHIFT ");
    if (Keyboard.PrimaryDevice.IsKeyDown(Key.LeftCtrl))
        sb.Append("LEFT CTRL ");
    if (Keyboard.PrimaryDevice.IsKeyDown(Key.RightCtrl))
        sb.Append("RIGHT CTRL ");
    Debug.Print(sb.ToString());

In your example, you are using a variable "e" which seemingly points toward EventArgs. If that is the case, e might provide the modifierCollection. (I didn´t check since I don´t know which event you are using). If it does, you should use that one instead of "Keyboard.PrimaryDevice.Modifiers".

After trying some things out, I noticed that it did not work on my computer as well if I use it in the KeyDown Eventhandler and there the e.KeyboardDevice Property. The reason for this is, that the eventHandler takes your keyboard layout into consideration. Since a non US Layout has no Right Alt (Alt GR is registered as "ALT + LEFT CTRL" by windows) this will never work. I tried changing my Layout to English-US and it worked perfectly.

So in short: To check for ALT-GR you will have to check against "ALT Modifier + CTRL Modifier".

    private void WindowKeyDown(object sender, KeyEventArgs e)
    {
        var alt = e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Alt);
        var ctrl = e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Control);
        var altGr = alt & ctrl;
        var shift = e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Shift);

        if (altGr & shift)
            MessageBox.Show("Shift+AlgGr");
    }

However, there is no difference between "CTRL + ALT + SHIFT" and "ALT-GR + SHIFT" because there really isn´t one. The operating system reports ALT-GR as ALT + CTRL. The only way I see to work around this, would be by using the US Layout in your program. That however would probably result in a couple other problems, depending on what you are doing.