Can anyone tell me the difference between the KeyDown
event, the KeyPress
event and the KeyUp
event? I checked the msdn site and it do
KeyDown
then KeyPress
then KeyUp
is the order I find.
Usually you want to hook KeyDown
when it is for an application where a user holds down a key for multi-mode input with control key mode modification, like in a shift-click operation. KeyPress
is for simple key entry type logic -- just getting the key strokes. KeyUp
is hooked to put in logic that executes after something else processes KeyPress
, like to modify the contents of a text edit box after it's main KeyPress
logic has taken effect. Frankly, I don't use KeyUp
all that much, but sometimes it is the only way to get a message after something else has processed KeyPress
and you need to check on / fixup what happened.
In addition to the other answers:
When trying to figure to which of these events you should hookup your action, mind that the KeyDown
event will be fired multiple times while the key is held down. Sometimes you want this behavior, sometimes not. Based on that I suggest the following usage (based on my experience):
(Order in which events are fired)
KeyDown
Occurs: When key is pressed and while held down
Usage: Perform action immediately on button press or even multiple times when held down
Example: Moving cursor with arrow keys
.
KeyPress
Occurs: Character key is pressed (Higher level event)
Usage: Anything typing related
Example: Handle textbox input
.
KeyUp
Occurs: Key is released
Usage: Perform critical action which should only occur once per keystroke
Example: Write data to file
Here's a case when you DON'T want to use KeyUp:
You have a list box and pressing the Enter key on a row invokes an editor dialog. Problem: If the user presses the Enter key on the OK button of the editor, a KeyUp(e.KeyCode=Enter) event will leak back to your list box, causing the editor to reopen. This doesn't happen if the user presses the space bar on the editor's OK button; in that case the KeyUp(e.KeyCode=Space) event is handled by the editor before it closes.
Here's a selection heuristic I use:
If I'm handling the Enter key and I need to guard against a case like the one above
then I use KeyDown
Else if I'm handling key combinations (e.g. CTRL+C)
then I favor* KeyDown (KeyUp can make these awkward)
Else if I am allowing press & hold autorepeat
then I use KeyDown
Else
I use KeyUp
*If the action is a one that can be done in a commonly used product, say Microsoft Office, such as CTRL+A (for 'Select All'), then I mimic the Microsoft behavior, since that is what users are used to.
The MSDN documentation states the order in which the three events occur fairly clearly:
Key events occur in the following order:
- KeyDown
- KeyPress
- KeyUp
KeyDown is raised as soon as the user presses a key on the keyboard, while they're still holding it down.
KeyPress is raised for character keys (unlike KeyDown and KeyUp, which are also raised for noncharacter keys) while the key is pressed. This is a "higher-level" event than either KeyDown or KeyUp, and as such, different data is available in the EventArgs
.
KeyUp is raised after the user releases a key on the keyboard.
Generally, you should handle the KeyUp
event in your application. Actions should not be initiated in the UI until after the user releases the key. And since KeyUp
is a lower-level event than KeyPress
, you'll always have plenty of information at your fingertips about the key that was pressed, and it will even work for handling non-character keys.
The thing to note about all of these events, however, is that they are only raised by the control that has the focus. That means if a button control on your form currently has the focus, none of the key events for your form will ever get raised. This is often confusing for programmers new to .NET. The best way to handle this is by overriding the form's ProcessCmdKey method:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Control | Keys.A))
{
MessageBox.Show("You pressed Ctrl+A!");
}
return base.ProcessCmdKey(ref msg, keyData);
}
KeyDown: happens when the person presses a key (when the keyboard first detects a finger on a key, this happens when the key is pressed down).
KeyPress: happens when a key is pressed and then released.
KeyUp: happens when the key is released
You are right that all of these events occur when a key is pressed and then released, in the order I described above.