Identify two modifier keys pressed

0

How to identify which two keys are pressed inside a click event of a button? 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");
    }
}
    
asked by anonymous 05.02.2018 / 17:19

1 answer

2

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

In 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 Modifiers in KeyEventArgs .

if (e.Modifiers == (Keys.Control | Keys.Shift))
{
    //Control e Shift pressionados
}
    
05.02.2018 / 17:23