--- title: "How to: Modify Keyboard Input to a Standard Control" ms.date: "03/30/2017" dev_langs: - "csharp" - "vb" helpviewer_keywords: - "keyboard input [Windows Forms], modifying" - "modifying keyboard input" - "Windows Forms, modifying keyboard input" - "keyboards [Windows Forms], keyboard input" ms.assetid: 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 event handler, set the property of the class to `true`. -or- In a event handler, set the property of the class to `true`. > [!NOTE] > Setting the property in the event handler does not prevent the and events from being raised for the current keystroke. Use the property for this purpose. The following example is an excerpt from a `switch` statement that examines the property of the received by a event handler. This code consumes the 'A' and 'a' character keys. [!code-csharp[System.Windows.Forms.KeyBoardInput#6](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.KeyboardInput/CS/form1.cs#6)] [!code-vb[System.Windows.Forms.KeyBoardInput#6](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.KeyboardInput/VB/form1.vb#6)] ### To modify a standard character key - In a event handler, set the property of the 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 property of the parameter is set to `false`, so that the new key value is propagated to other methods and events in the message queue. [!code-csharp[System.Windows.Forms.KeyBoardInput#7](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.KeyboardInput/CS/form1.cs#7)] [!code-vb[System.Windows.Forms.KeyBoardInput#7](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.KeyboardInput/VB/form1.vb#7)] ### To modify a noncharacter key - Override a method that processes Windows messages, detect the WM_KEYDOWN or WM_SYSKEYDOWN message, and set the property of the parameter to the value that represents the new noncharacter key. The following code example demonstrates how to override the method of a control to detect keys F1 through F9 and modify any F3 key press to F1. For more information on methods that you can override to intercept keyboard messages, see [User Input in a Windows Forms Application](user-input-in-a-windows-forms-application.md) and [How Keyboard Input Works](how-keyboard-input-works.md). [!code-csharp[System.Windows.Forms.KeyBoardInput#12](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.KeyboardInput/CS/form1.cs#12)] [!code-vb[System.Windows.Forms.KeyBoardInput#12](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.KeyboardInput/VB/form1.vb#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 class to consume and modify keyboard input. [!code-csharp[System.Windows.Forms.KeyBoardInput#0](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.KeyboardInput/CS/form1.cs#0)] [!code-vb[System.Windows.Forms.KeyBoardInput#0](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.KeyboardInput/VB/form1.vb#0)] ## Compiling the Code This example requires: - References to the System, System.Drawing and System.Windows.Forms assemblies. ## See also - [Keyboard Input in a Windows Forms Application](keyboard-input-in-a-windows-forms-application.md) - [User Input in a Windows Forms Application](user-input-in-a-windows-forms-application.md) - [How Keyboard Input Works](how-keyboard-input-works.md)