简体   繁体   English

如何在WPF / C#中区分多个组合键

[英]How to distinguish between multiple key combination in wpf/c#

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. 我会模拟所有键盘功能,但我无法区分一个修改器+键和2个修改器+键。

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. 例如,如果我按shift +键它的工作,但如果我按shift + ALTGR +键不工作becouse我的程序只检测ALTGR即使当按下Shift我检查。

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. 谢谢你的每一个想法。

    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. 在您的示例中,您使用的变量“ e”似乎指向EventArgs。 If that is the case, e might provide the modifierCollection. 如果是这样,e可能会提供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". 如果是这样,则应使用那个而不是“ 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. 在尝试了一些方法之后,我注意到,如果我在KeyDown Eventhandler和e.KeyboardDevice属性中使用它,则它也无法在我的计算机上正常工作。 The reason for this is, that the eventHandler takes your keyboard layout into consideration. 这样做的原因是,eventHandler考虑了您的键盘布局。 Since a non US Layout has no Right Alt (Alt GR is registered as "ALT + LEFT CTRL" by windows) this will never work. 由于非美国版式没有右Alt(Alt GR在Windows中被注册为“ ALT +左CTRL”),因此它将永远无法使用。 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". 简而言之:要检查ALT-GR,您必须对照“ ALT修饰符+ CTRL修饰符”进行检查。

    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. 但是,“ CTRL + ALT + SHIFT”和“ ALT-GR + SHIFT”之间没有区别,因为实际上没有一个。 The operating system reports ALT-GR as ALT + CTRL. 操作系统将ALT-GR报告为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. 但是,这可能会导致其他一些问题,具体取决于您正在执行的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM