Identify two pressed modifier keys

Asked

Viewed 139 times

0

How to identify which two keys are pressed inside a button click event? Example:

Button btn = new Button();
btn.Click += Btn_Click;

private void Btn_Click(object sender, EventArgs e)
{
    // Assim consigo identificar que uma tecla está pressionada.
    // Gostaria de identificar o Control e Shift
    if (Control.ModifierKeys == Keys.Control)
    {
         MessageBox("Control pressionado");
    }
}

1 answer

2


The guy Keys is a mask of bits (a combination of bits)

This way, you can use binary operations to check if more than one modifier is pressed

if (Control.ModifierKeys == (Keys.Control | Keys.Shift))
{
     MessageBox("Control e Shift pressionados");
}

Note that you can use the property Modifiers in KeyEventArgs.

if (e.Modifiers == (Keys.Control | Keys.Shift))
{
    //Control e Shift pressionados
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.