Skip to content

Latest commit

 

History

History
70 lines (46 loc) · 5.54 KB

how-to-modify-keyboard-input-to-a-standard-control.md

File metadata and controls

70 lines (46 loc) · 5.54 KB
title description ms.date dev_langs helpviewer_keywords ms.assetid
How to: Modify Keyboard Input to a Standard Control
Learn how to modify keyboard input to a standard Windows Forms control so that you can consume and modify keyboard input.
03/30/2017
csharp
vb
keyboard input [Windows Forms], modifying
modifying keyboard input
Windows Forms, modifying keyboard input
keyboards [Windows Forms], keyboard input
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 switch statement 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 switch statement 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 to false, 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.

See also