--- title: "How Keyboard Input Works" ms.date: "03/30/2017" helpviewer_keywords: - "keyboard input [Windows Forms], about keyboard input" - "keyboards [Windows Forms], keyboard input" - "Windows Forms, keyboard input" ms.assetid: 9a29433c-a180-49bb-b74c-d187786584c8 --- # How Keyboard Input Works Windows Forms processes keyboard input by raising keyboard events in response to Windows messages. Most Windows Forms applications process keyboard input exclusively by handling the keyboard events. However, you need to understand how keyboard messages work so you can implement more advanced keyboard-input scenarios, such as intercepting keys before they reach a control. This topic describes the types of key data that Windows Forms recognizes and provides an overview of how keyboard messages are routed. For information about keyboard events, see [Using Keyboard Events](using-keyboard-events.md). ## Types of Keys Windows Forms identifies keyboard input as virtual-key codes that are represented by the bitwise enumeration. With the enumeration, you can combine a series of pressed keys to result in a single value. These values correspond to the values that accompany the WM_KEYDOWN and WM_SYSKEYDOWN Windows messages. You can detect most physical key presses by handling the or events. Character keys are a subset of the enumeration and correspond to the values that accompany the WM_CHAR and WM_SYSCHAR Windows messages. If the combination of pressed keys results in a character, you can detect the character by handling the event. Alternatively, you can use , exposed by Visual Basic programming interface, to discover which keys were pressed and send keys. For more information, see [Accessing the Keyboard](https://docs.microsoft.com/dotnet/visual-basic/developing-apps/programming/computer-resources/accessing-the-keyboar). ## Order of Keyboard Events As listed previously, there are 3 keyboard related events that can occur on a control. The following sequence shows the general order of the events: 1. The user pushes the "a" key, the key is preprocessed, dispatched, and a event occurs. 2. The user holds the "a" key, the key is preprocessed, dispatched, and a event occurs. This event occurs multiple times as the user holds a key. 3. The user releases the "a" key, the key is preprocessed, dispatched and a event occurs. ## Preprocessing Keys Like other messages, keyboard messages are processed in the method of a form or control. However, before keyboard messages are processed, the method calls one or more methods that can be overridden to handle special character keys and physical keys. You can override these methods to detect and filter certain keys before the messages are processed by the control. The following table shows the action that is being performed and the related method that occurs, in the order that the method occurs. ### Preprocessing for a KeyDown event |Action|Related method|Notes| |------------|--------------------|-----------| |Check for a command key such as an accelerator or menu shortcut.||This method processes a command key, which takes precedence over regular keys. If this method returns `true`, the key message is not dispatched and a key event does not occur. If it returns `false`, is called`.`| |Check for a special key that requires preprocessing or a normal character key that should raise a event and be dispatched to a control.||If the method returns `true`, it means the control is a regular character and a event is raised. If `false`, is called. **Note:** To ensure a control gets a key or combination of keys, you can handle the event and set of the to `true` for the key or keys you want.| |Check for a navigation key (ESC, TAB, Return, or arrow keys).||This method processes a physical key that employs special functionality within the control, such as switching focus between the control and its parent. If the immediate control does not handle the key, the is called on the parent control and so on to the topmost control in the hierarchy. If this method returns `true`, preprocessing is complete and a key event is not generated. If it returns `false`, a event occurs.| ### Preprocessing for a KeyPress Event |Action|Related method|Notes| |------------|--------------------|-----------| |Check to see the key is a normal character that should be processed by the control||If the character is a normal character, this method returns `true`, the event is raised and no further preprocessing occurs. Otherwise will be called.| |Check to see if the character is a mnemonic (such as &OK on a button)||This method, similar to , will be called up the control hierarchy. If the control is a container control, it checks for mnemonics by calling on itself and its child controls. If returns `true`, a event does not occur.| ## Processing Keyboard Messages After keyboard messages reach the method of a form or control, they are processed by a set of methods that can be overridden. Each of these methods returns a value specifying whether the keyboard message has been processed and consumed by the control. If one of the methods returns `true`, then the message is considered handled, and it is not passed to the control's base or parent for further processing. Otherwise, the message stays in the message queue and may be processed in another method in the control's base or parent. The following table presents the methods that process keyboard messages. |Method|Notes| |------------|-----------| ||This method processes all keyboard messages that are received by the method of the control.| ||This method sends the keyboard message to the control's parent. If returns `true`, no key event is generated, otherwise is called.| ||This method raises the , , and events, as appropriate.| ## Overriding Keyboard Methods There are many methods available for overriding when a keyboard message is preprocessed and processed; however, some methods are much better choices than others. Following table shows tasks you might want to accomplish and the best way to override the keyboard methods. For more information on overriding methods, see [Overriding properties and methods in derived classes](https://docs.microsoft.com/dotnet/visual-basic/programming-guide/language-features/objects-and-classes/inheritance-basics#overriding-properties-and-methods-in-derived-classes). |Task|Method| |----------|------------| |Intercept a navigation key and raise a event. For example you want TAB and Return to be handled in a text box.|Override . **Note:** Alternatively, you can handle the event and set of the to `true` for the key or keys you want.| |Perform special input or navigation handling on a control. For example, you want the use of arrow keys in your list control to change the selected item.|Override | |Intercept a navigation key and raise a event. For example in a spin-box control you want multiple arrow key presses to accelerate progression through the items.|Override .| |Perform special input or navigation handling during a event. For example, in a list control holding down the "r" key skips between items that begin with the letter r.|Override | |Perform custom mnemonic handling; for example, you want to handle mnemonics on owner-drawn buttons contained in a toolbar.|Override .| ## See also - - - - [My.Computer.Keyboard Object](https://docs.microsoft.com/dotnet/visual-basic/language-reference/objects/my-computer-keyboard-object) - [Accessing the Keyboard](https://docs.microsoft.com/dotnet/visual-basic/developing-apps/programming/computer-resources/accessing-the-keyboar) - [Using Keyboard Events](using-keyboard-events.md)