如何在WPF/C#中区分多个组合键 [英] How to distinguish between multiple key combination in wpf/c#

查看:282
本文介绍了如何在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.

例如,如果我按 shift +键,它可以工作,但是如果我按 shift + altGR +键,则不起作用,因为我即使我检查是否按了 shift ,程序也只会检测 altGR .

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.

推荐答案

    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());

这应该是您所需要的.

您可以使用以下方法检查特定键:

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());

在您的示例中,您使用的变量"e"似乎指向EventArgs.在这种情况下,e可能会提供modifierCollection. (我不检查,因为我不知道您正在使用哪个事件).如果是这样,您应该使用那个而不是"Keyboard.PrimaryDevice.Modifiers".

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".

尝试了一些事情之后,我注意到,如果我在KeyDown Eventhandler中使用它,并且在e.KeyboardDevice属性中使用它,则它也无法在我的计算机上运行.这样做的原因是,eventHandler考虑了您的键盘布局.由于非美国版式没有右Alt(Alt GR在Windows中被注册为"ALT +左CTRL"),因此将永远无法使用.我尝试将版式更改为英语-美国",并且效果很好.

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.

因此,简而言之:要检查ALT-GR,您必须检查"ALT修饰符+ CTRL修饰符".

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");
    }

但是,"CTRL + ALT + SHIFT"和"ALT-GR + SHIFT"之间没有区别,因为实际上没有一个.操作系统将ALT-GR报告为ALT + CTRL.我认为解决此问题的唯一方法是在程序中使用美国布局".但是,这可能会导致其他一些问题,具体取决于您的工作.

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.

这篇关于如何在WPF/C#中区分多个组合键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆