What is the difference between the KeyDown
and KeyPress
events in .NET?
10 Answers
There is apparently a lot of misunderstanding about this!
The only practical difference between KeyDown
and KeyPress
is that KeyPress
relays the character resulting from a keypress, and is only called if there is one.
In other words, if you press A on your keyboard, you'll get this sequence of events:
- KeyDown: KeyCode=Keys.A, KeyData=Keys.A, Modifiers=Keys.None
- KeyPress: KeyChar='a'
- KeyUp: KeyCode=Keys.A
But if you press Shift+A, you'll get:
- KeyDown: KeyCode=Keys.ShiftKey, KeyData=Keys.ShiftKey, Shift, Modifiers=Keys.Shift
- KeyDown: KeyCode=Keys.A, KeyData=Keys.A | Keys.Shift, Modifiers=Keys.Shift
- KeyPress: KeyChar='A'
- KeyUp: KeyCode=Keys.A
- KeyUp: KeyCode=Keys.ShiftKey
If you hold down the keys for a while, you'll get something like:
- KeyDown: KeyCode=Keys.ShiftKey, KeyData=Keys.ShiftKey, Shift, Modifiers=Keys.Shift
- KeyDown: KeyCode=Keys.ShiftKey, KeyData=Keys.ShiftKey, Shift, Modifiers=Keys.Shift
- KeyDown: KeyCode=Keys.ShiftKey, KeyData=Keys.ShiftKey, Shift, Modifiers=Keys.Shift
- KeyDown: KeyCode=Keys.ShiftKey, KeyData=Keys.ShiftKey, Shift, Modifiers=Keys.Shift
- KeyDown: KeyCode=Keys.ShiftKey, KeyData=Keys.ShiftKey, Shift, Modifiers=Keys.Shift
- KeyDown: KeyCode=Keys.A, KeyData=Keys.A | Keys.Shift, Modifiers=Keys.Shift
- KeyPress: KeyChar='A'
- KeyDown: KeyCode=Keys.A, KeyData=Keys.A | Keys.Shift, Modifiers=Keys.Shift
- KeyPress: KeyChar='A'
- KeyDown: KeyCode=Keys.A, KeyData=Keys.A | Keys.Shift, Modifiers=Keys.Shift
- KeyPress: KeyChar='A'
- KeyDown: KeyCode=Keys.A, KeyData=Keys.A | Keys.Shift, Modifiers=Keys.Shift
- KeyPress: KeyChar='A'
- KeyDown: KeyCode=Keys.A, KeyData=Keys.A | Keys.Shift, Modifiers=Keys.Shift
- KeyPress: KeyChar='A'
- KeyUp: KeyCode=Keys.A
- KeyUp: KeyCode=Keys.ShiftKey
Notice that KeyPress
occurs in between KeyDown
and KeyUp
, not after KeyUp
, as many of the other answers have stated, that KeyPress
is not called when a character isn't generated, and that KeyDown
is repeated while the key is held down, also contrary to many of the other answers.
Examples of keys that do not directly result in calls to KeyPress
:
- Shift, Ctrl, Alt
- F1 through F12
- Arrow keys
Examples of keys that do result in calls to KeyPress
:
- A through Z, 0 through 9, etc.
- Spacebar
- Tab (KeyChar='\t', ASCII 9)
- Enter (KeyChar='\r', ASCII 13)
- Esc (KeyChar='\x1b', ASCII 27)
- Backspace (KeyChar='\b', ASCII 8)
For the curious, KeyDown
roughly correlates to WM_KEYDOWN
, KeyPress
to WM_CHAR
, and KeyUp
to WM_KEYUP
. WM_KEYDOWN
can be called fewer than the the number of key repeats, but it sends a repeat count, which, IIRC, WinForms uses to generate exactly one KeyDown per repeat.
The KeyPress event is not raised by noncharacter keys; however, the noncharacter keys do raise the KeyDown and KeyUp events.
https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.control.keypress
I've always thought keydown happened as soon as you press the key down, keypress is the action of pressing the key and releasing it.
I found this which gives a little different explanation: http://bytes.com/topic/net/answers/649131-difference-keypress-keydown-event
From Blogging Developer:
In order to understand the difference between keydown and keypress, it is useful to understand the difference between a "character" and a "key". A "key" is a physical button on the computer's keyboard while a "character" is a symbol typed by pressing a button. In theory, the keydown and keyup events represent keys being pressed or released, while the keypress event represents a character being typed. The implementation of the theory is not same in all browsers.
Note: You can also try out the Key Event Tester (available on the above-mentioned site) to understand this concept.