* Migrate inprogress winforms contnet from docs * Fix preview note * Fix vb code * Minor fixes to keyboard articles * Minor adjustment to overview * Test redirects for 4.0 -> 5.0 * adjust links * Add mouse input section winforms (#81) * Update projects to .NET 5 * Update keyboard desc * Finish mouse events * Add remaining mouse articles;code * finish mouse input * minor fixes * Remove branch restriction (#82) * Update build-validation.yml (#84) * Update build-validation.yml (#85) * Fix vb proj * Add WinForms tutorial (#91) * redirect between overview * New create an app tutorial * Fix markdown * Fix preserve view setting * Add forms articles (#93) * Add forms articles * Fix warnings * Winforms publish (#95) * Prep TOC for publish * Updated date * Automatic how-to topic type * Update desc/headers * Fix links * Fix build errors * Add preview note * Update toc landing page * Convert old style project files * update download links * Apply suggestions from code review Co-authored-by: Genevieve Warren <[email protected]> * Adjust number key * Update dotnet-desktop-guide/net/winforms/overview/index.md * Try toc position task via bookmark * Improve images * Remove sentence * File redirects Co-authored-by: Genevieve Warren <[email protected]>
4.1 KiB
title, description, ms.date, dev_langs, helpviewer_keywords
| 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 |
|
|
How to check for modifier key presses (Windows Forms .NET)
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.
[!INCLUDE desktop guide under construction]
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 modifier key
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":::