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

前端 未结 2 1871
北海茫月
北海茫月 2021-01-06 15:55

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

I did the following, but I\'m not too familiar with these operators so I

相关标签:
2条回答
  • 2021-01-06 16:19

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

    0 讨论(0)
  • 2021-01-06 16:37

    according to msdn linkt

    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");
            }
        }
    
    0 讨论(0)
提交回复
热议问题