Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Most Windows Forms programs process keyboard input by handling the keyboard events. This article provides an overview of the keyboard events, including details on when to use each event and the data that each event provides. For more information about events in general, see Events overview.
Keyboard events
Windows Forms raises the following events when a user presses and releases a keyboard key:
When a user presses a key, Windows Forms determines which event to raise based on whether the keyboard message specifies a character key or a physical key. For more information about character and physical keys, see Keyboard overview, keyboard events.
KeyDown event
The KeyDown event is raised when a user presses a physical key. If the key is held down, this event repeats at the OS keyboard repeat rate.
The handler for KeyDown receives a KeyEventArgs parameter that provides:
- The KeyCode property, which specifies a physical keyboard button.
- The Modifiers property (
Shift,Ctrl, orAlt). - The KeyData property, which combines the key code and modifier.
- The Handled property, which prevents the underlying control from receiving the key when set.
- The SuppressKeyPress property, which suppresses the KeyPress and KeyUp events for that keystroke.
KeyPress event
The KeyPress event is raised when the key or keys pressed result in a character. For example, pressing Shift and the lowercase "a" key produces a capital letter "A" character. KeyPress is raised after KeyDown and repeats at the OS keyboard repeat rate while the key is held.
The handler for KeyPress receives a KeyPressEventArgs parameter that contains the character code of the key pressed. This character code is unique for every combination of a character key and a modifier key.
For example, the "A" key generates:
- The character code 65, if it's pressed with the Shift key.
- The character code 65, if Caps Lock is on.
- The character code 97, if it's pressed by itself.
- The character code 1, if it's pressed with the Ctrl key.
KeyUp event
The KeyUp event is raised once when a user releases a physical key.
The handler for KeyUp receives a KeyEventArgs parameter that provides:
- The KeyCode property, which specifies a physical keyboard button.
- The Modifiers property (
Shift,Ctrl, orAlt). - The KeyData property, which combines the key code and modifier.
See also
.NET Desktop feedback