C #: In the keydown event of a text field, how do you detect the current pressed modifiers + keys?

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

I did the following, but I'm not very familiar with these operators, so I'm not sure what 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 to check if e contains modifier keys?

+3
source share
2 answers

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

The documentation says this is a bitwise combination, but you do 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 ().

+2
source

Source: https://habr.com/ru/post/1705238/


All Articles