C#: Getting the correct keys pressed from KeyEventArgs' KeyData

后端 未结 3 528
闹比i
闹比i 2021-02-04 21:01

I am trapping a KeyDown event and I need to be able to check whether the current keys pressed down are : Ctrl + Shift + M ?

<
相关标签:
3条回答
  • 2021-02-04 21:50

    You can check using a technique similar to the following:

    if(Control.ModifierKeys == Keys.Control && Control.ModifierKeys == Keys.Shift)
    

    This in combination with the normal key checks will give you the answer you seek.

    0 讨论(0)
  • 2021-02-04 21:51

    I think its easiest to use this:

    if(e.KeyData == (Keys.Control | Keys.G))

    0 讨论(0)
  • 2021-02-04 21:52

    You need to use the Modifiers property of the KeyEventArgs class.

    Something like:

    //asumming e is of type KeyEventArgs (such as it is 
    // on a KeyDown event handler
    // ..
    bool ctrlShiftM; //will be true if the combination Ctrl + Shift + M is pressed, false otherwise
    
    ctrlShiftM = ((e.KeyCode == Keys.M) &&               // test for M pressed
                  ((e.Modifiers & Keys.Shift) != 0) &&   // test for Shift modifier
                  ((e.Modifiers & Keys.Control) != 0));  // test for Ctrl modifier
    if (ctrlShiftM == true)
    {
        Console.WriteLine("[Ctrl] + [Shift] + M was pressed");
    }
    
    0 讨论(0)
提交回复
热议问题