Home > Software engineering >  Handling multiple keys inside a switch block
Handling multiple keys inside a switch block

Time:12-22

I am developing a Windows Forms Application where I am trying to Hide a panel on whenever a user presses the combination of F12 and ctrl key but I am getting the error Operator '&&' cannot be applied to operands of type 'Keys' and 'Keys' . Thanks for your time.

private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        //method to assign keys
        switch (e.KeyCode)
        {
            case Keys.Down:                   
                SendKeys.Send("{Tab}");
                e.Handled = true;
                break;

            case (Keys.Control && Keys.F12): **// error here** 
                 this.panel3.Hide();
            default:
                break;
        }
    }

CodePudding user response:

If you want to Hide() on Ctrl F12 combination, you should check e.Modifiers:

...

case (Keys.F12): // On F12
  if (e.Modifiers == Keys.Control) { // On Ctrl   F12
    this.panel3.Hide();
  } 

...
  • Related