* Merge winforms framework content to working branch (#14) * Breadcrumb / TOC / Move net5 to net folder (#15) * change path from net5 to net * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Add .net 5 winforms placeholder article (#16) * added some metadata and adjusted net5 placeholder * corrections * corrections * corrections * Test1 * Swapping landing page vs concept * fix links * Fix links * Fix desc
5.4 KiB
title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
| title | ms.date | dev_langs | helpviewer_keywords | ms.assetid | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| How to: Modify Keyboard Input to a Standard Control | 03/30/2017 |
|
|
626d3712-d866-4988-bcda-a2d5b36ec0ba |
How to: Modify Keyboard Input to a Standard Control
Windows Forms provides the ability to consume and modify keyboard input. Consuming a key refers to handling a key within a method or event handler so that other methods and events further down the message queue do not receive the key value. Modifying a key refers to modifying the value of a key so that methods and event handlers further down the message queue receive a different key value. This topic shows how to accomplish these tasks.
To consume a key
-
In a xref:System.Windows.Forms.Control.KeyPress event handler, set the xref:System.Windows.Forms.KeyPressEventArgs.Handled%2A property of the xref:System.Windows.Forms.KeyPressEventArgs class to
true.-or-
In a xref:System.Windows.Forms.Control.KeyDown event handler, set the xref:System.Windows.Forms.KeyEventArgs.Handled%2A property of the xref:System.Windows.Forms.KeyEventArgs class to
true.Note
Setting the xref:System.Windows.Forms.KeyEventArgs.Handled%2A property in the xref:System.Windows.Forms.Control.KeyDown event handler does not prevent the xref:System.Windows.Forms.Control.KeyPress and xref:System.Windows.Forms.Control.KeyUp events from being raised for the current keystroke. Use the xref:System.Windows.Forms.KeyEventArgs.SuppressKeyPress%2A property for this purpose.
The following example is an excerpt from a
switchstatement that examines the xref:System.Windows.Forms.KeyPressEventArgs.KeyChar%2A property of the xref:System.Windows.Forms.KeyPressEventArgs received by a xref:System.Windows.Forms.Control.KeyPress event handler. This code consumes the 'A' and 'a' character keys.[!code-csharpSystem.Windows.Forms.KeyBoardInput#6] [!code-vbSystem.Windows.Forms.KeyBoardInput#6]
To modify a standard character key
-
In a xref:System.Windows.Forms.Control.KeyPress event handler, set the xref:System.Windows.Forms.KeyPressEventArgs.KeyChar%2A property of the xref:System.Windows.Forms.KeyPressEventArgs class to the value of the new character key.
The following example is an excerpt from a
switchstatement that modifies 'B' to 'A' and 'b' to 'a'. Note that the xref:System.Windows.Forms.KeyPressEventArgs.Handled%2A property of the xref:System.Windows.Forms.KeyPressEventArgs parameter is set tofalse, so that the new key value is propagated to other methods and events in the message queue.[!code-csharpSystem.Windows.Forms.KeyBoardInput#7] [!code-vbSystem.Windows.Forms.KeyBoardInput#7]
To modify a noncharacter key
-
Override a xref:System.Windows.Forms.Control method that processes Windows messages, detect the WM_KEYDOWN or WM_SYSKEYDOWN message, and set the xref:System.Windows.Forms.Message.WParam%2A property of the xref:System.Windows.Forms.Message parameter to the xref:System.Windows.Forms.Keys value that represents the new noncharacter key.
The following code example demonstrates how to override the xref:System.Windows.Forms.Control.PreProcessMessage%2A method of a control to detect keys F1 through F9 and modify any F3 key press to F1. For more information on xref:System.Windows.Forms.Control methods that you can override to intercept keyboard messages, see User Input in a Windows Forms Application and How Keyboard Input Works.
[!code-csharpSystem.Windows.Forms.KeyBoardInput#12] [!code-vbSystem.Windows.Forms.KeyBoardInput#12]
Example
The following code example is the complete application for the code examples in the previous sections. The application uses a custom control derived from the xref:System.Windows.Forms.TextBox class to consume and modify keyboard input.
[!code-csharpSystem.Windows.Forms.KeyBoardInput#0] [!code-vbSystem.Windows.Forms.KeyBoardInput#0]
Compiling the Code
This example requires:
- References to the System, System.Drawing and System.Windows.Forms assemblies.