title | description | ms.date | dev_langs | helpviewer_keywords | |||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Check which modifier key is pressed |
Learn how to detect when the SHIFT, ALT, or CTRL keys are pressed in Windows Forms for .NET. |
10/26/2020 |
|
|
As the user types keys into your application, you can monitor for pressed modifier keys such as the SHIFT, ALT, and CTRL. When a modifier key is pressed in combination with other keys or even a mouse click, your application can respond appropriately. For example, pressing the S key may cause an "s" to appear on the screen. If the keys CTRL+S are pressed, instead, the current document may be saved.
If you handle the xref:System.Windows.Forms.Control.KeyDown event, the xref:System.Windows.Forms.KeyEventArgs.Modifiers?displayProperty=nameWithType property received by the event handler specifies which modifier keys are pressed. Also, the xref:System.Windows.Forms.KeyEventArgs.KeyData?displayProperty=nameWithType property specifies the character that was pressed along with any modifier keys combined with a bitwise OR.
If you're handling the xref:System.Windows.Forms.Control.KeyPress event or a mouse event, the event handler doesn't receive this information. Use the xref:System.Windows.Forms.Control.ModifierKeys%2A property of the xref:System.Windows.Forms.Control class to detect a key modifier. In either case, you must perform a bitwise AND of the appropriate xref:System.Windows.Forms.Keys value and the value you're testing. The xref:System.Windows.Forms.Keys enumeration offers variations of each modifier key, so it's important that you do the bitwise AND check with the correct value.
For example, the SHIFT key is represented by the following key values:
- xref:System.Windows.Forms.Keys.Shift?displayProperty=nameWithType
- xref:System.Windows.Forms.Keys.ShiftKey?displayProperty=nameWithType
- xref:System.Windows.Forms.Keys.RShiftKey?displayProperty=nameWithType
- xref:System.Windows.Forms.Keys.LShiftKey?displayProperty=nameWithType
The correct value to test SHIFT as a modifier key is xref:System.Windows.Forms.Keys.Shift?displayProperty=nameWithType. Similarly, to test for CTRL and ALT as modifiers you should use the xref:System.Windows.Forms.Keys.Control?displayProperty=nameWithType and xref:System.Windows.Forms.Keys.Alt?displayProperty=nameWithType values, respectively.
Detect if a modifier key is pressed by comparing the xref:System.Windows.Forms.Control.ModifierKeys%2A property and the xref:System.Windows.Forms.Keys enumeration value with a bitwise AND operator.
The following code example shows how to determine whether the SHIFT key is pressed within the xref:System.Windows.Forms.Control.KeyPress and xref:System.Windows.Forms.Control.KeyDown event handlers.
:::code language="csharp" source="snippets/how-to-check-modifier-key/csharp/Form1.cs" id="DetectModifier"::: :::code language="vb" source="snippets/how-to-check-modifier-key/vb/Form1.vb" id="DetectModifier":::
- Overview of using the keyboard (Windows Forms .NET)
- Using keyboard events (Windows Forms .NET)
- xref:System.Windows.Forms.Keys
- xref:System.Windows.Forms.Control.ModifierKeys
- xref:System.Windows.Forms.Control.KeyDown
- xref:System.Windows.Forms.Control.KeyPress