简体   繁体   English

C#:在文本框的 keydown 事件中,如何检测当前按下的修饰符 + 键?

[英]C#: In the keydown event of a textbox, how do you detect currently pressed modifiers + keys?

C#: In the keydown event of a textbox, how do you detect currently pressed modifiers + keys? C#:在文本框的 keydown 事件中,如何检测当前按下的修饰符 + 键?

I did the following, but I'm not too familiar with these operators so I'm not sure if and what I'm doing wrong.我做了以下操作,但我对这些运算符不太熟悉,所以我不确定我是否以及我做错了什么。

    private void txtShortcut_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Modifiers == (Keys.Alt || Keys.Control || Keys.Shift))
        {
            lbLogger.Items.Add(e.Modifiers.ToString() + " + " +
                "show non-modifier key being pressed here");
        }
    }

1) How would I check if e contains any modifier keys? 1)如何检查 e 是否包含任何修饰键?

according to msdn linkt根据msdn链接

private void txtShortcut_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Alt || e.Control || e.Shift))
        {
            lbLogger.Items.Add(e.ToString() + " + " +
                "show non-modifier key being pressed here");
        }
    }

The documentation says it is a bitwise combination, but you're doing a logical OR. 文档说它是按位组合,但你正在做一个逻辑或。

Try:尝试:

if (e.Modifiers & (Keys.Alt | Keys.Control | Keys.Shift))
{
    lbLogger.Items.Add(e.Modifiers.ToString() + " + " +
        "show non-modifier key being pressed here");
}

And I think you can get the actual key with e.KeyCode.ToString().而且我认为您可以使用 e.KeyCode.ToString() 获取实际密钥。

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

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