4

I know how to obtain which modifier key was pressed in C# but I don't know how I can actually check if any modifier key was pressed. I need to check it in KeyUp event, is it possible any other way than doing something like if(e.KeyCode != Keys.Control && e.KeyCode != Keys.Alt && ...) ? Thanks.

dav_i
  • 27,509
  • 17
  • 104
  • 136
haluzak
  • 1,133
  • 3
  • 17
  • 31

3 Answers3

12
if ((Control.ModifierKeys & Keys.Shift) != 0) 

will help you detect whether a modifier key (e.g. ctrl or shift) was pressed. Check the Post below for reference:

How to detect the currently pressed key?

Community
  • 1
  • 1
reggie
  • 13,313
  • 13
  • 41
  • 57
  • Replace the "&&" operator with the correct "&" operator. ("Error: Operator '&&' cannot be applied to operands of type 'System.Windows.Forms.Keys' and 'System.Windows.Forms.Keys') – TechAurelian Nov 22 '12 at 12:29
3

To make sure no modifier key is pressed you can check if ModifierKey equals Keys.None.

if (ModifierKeys == Keys.None) ...
caycothu
  • 31
  • 2
1

The KeyEventArgs class has properties that you can check. For example, to see if the Alt key was pressed, you can write:

if (e.Alt)
{
    // Alt key was pressed
}
Jim Mischel
  • 131,090
  • 20
  • 188
  • 351