tags:

views:

1346

answers:

8

What is the difference between the KeyDown and KeyPress events in .NET?

A: 

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 explaination: http://bytes.com/topic/net/answers/649131-difference-keypress-keydown-event

John Boker
+1  A: 

Keydown is pressing the key without releasing it, Keypress is a complete press-and-release cycle.

Put another way, KeyDown + KeyUp = Keypress

Rob Cowell
according to MSDN, the event sequence is KeyDown, KeyPress, KeyUp (http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress(VS.71).aspx)
Nathan Koop
Thanks for the clarification Nathan
Rob Cowell
+10  A: 

The KeyPress event is not raised by noncharacter keys; however, the noncharacter keys do raise the KeyDown and KeyUp events.

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress.aspx

Jon Spokes
Most of the other answers are wrong in one way or another, but this one comes closest to the reality. KeyPress is raised only for character keys and it obeys the setting of keyboard typing delays/repeating. The actual sequence of events is: 1) KeyDown 2) for character key KeyPress one or more times (depending on the system settings and how long the key is held) 3) KeyUp
Filip Navara
I don't have the rep to do it, but could someone bust a wiki and combine the content of the above comment and the accepted answer?
Josh Kodroff
Filip Navara's comment isn't entirely correct. If a key is held down, you'll get KeyDown, KeyPress, KeyDown, KeyPress, KeyDown, KeyPress KeyUp. KeyDown is called for every repeat.
P Daddy
You are right. Thanks for correcting me.
Filip Navara
+1  A: 

From MSDN:

Key events occur in the following order:

  1. KeyDown

  2. KeyPress

  3. KeyUp

Furthermore, KeyPress gives you a chance to declare the action as "handled" to prevent it from doing anything.

Jon B
+2  A: 

KeyPress is only fired by printable characters and is fired after the KeyDown event. Depending on the typing delay settings there can be multiple KeyDown and KeyPress events but only one KeyUp event.

KeyDown
KeyPress
KeyUp

Stevo3000
You're close, but off about the sequence.
P Daddy
@P Daddy - I've corrected it, should have double checked before going from memory.
Stevo3000
A: 

Easiest explanation:

I held down the 'd' key for a second and then released.

dddddd

the keydown event happened once before the first d appeared on the screen, the keypress event happened 6 times and the keyup event happened after the last d appeared on the screen.

Stephen lacy
This is incorrect. KeyDown is called for every key repeat.
P Daddy
damn, misread the question and got it into my head he meant javascript
Stephen lacy
+2  A: 

KeyPress is a higher level of abstraction than KeyDown (and KeyUp). KeyDown and KeyUp are hardware related: the actual action of a key on the keyboard. KeyPress is more "I received a character from the keyboard".

Jeff Hornby
+8  A: 

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:

  1. KeyDown: KeyCode=Keys.A, KeyData=Keys.A, Modifiers=Keys.None
  2. KeyPress: KeyChar='a'
  3. KeyUp: KeyCode=Keys.A

But if you press Shift+A, you'll get:

  1. KeyDown: KeyCode=Keys.ShiftKey, KeyData=Keys.ShiftKey, Shift, Modifiers=Keys.Shift
  2. KeyDown: KeyCode=Keys.A, KeyData=Keys.A | Keys.Shift, Modifiers=Keys.Shift
  3. KeyPress: KeyChar='A'
  4. KeyUp: KeyCode=Keys.A
  5. KeyUp: KeyCode=Keys.ShiftKey

If you hold down the keys for a while, you'll get something like:

  1. KeyDown: KeyCode=Keys.ShiftKey, KeyData=Keys.ShiftKey, Shift, Modifiers=Keys.Shift
  2. KeyDown: KeyCode=Keys.ShiftKey, KeyData=Keys.ShiftKey, Shift, Modifiers=Keys.Shift
  3. KeyDown: KeyCode=Keys.ShiftKey, KeyData=Keys.ShiftKey, Shift, Modifiers=Keys.Shift
  4. KeyDown: KeyCode=Keys.ShiftKey, KeyData=Keys.ShiftKey, Shift, Modifiers=Keys.Shift
  5. KeyDown: KeyCode=Keys.ShiftKey, KeyData=Keys.ShiftKey, Shift, Modifiers=Keys.Shift
  6. KeyDown: KeyCode=Keys.A, KeyData=Keys.A | Keys.Shift, Modifiers=Keys.Shift
  7. KeyPress: KeyChar='A'
  8. KeyDown: KeyCode=Keys.A, KeyData=Keys.A | Keys.Shift, Modifiers=Keys.Shift
  9. KeyPress: KeyChar='A'
  10. KeyDown: KeyCode=Keys.A, KeyData=Keys.A | Keys.Shift, Modifiers=Keys.Shift
  11. KeyPress: KeyChar='A'
  12. KeyDown: KeyCode=Keys.A, KeyData=Keys.A | Keys.Shift, Modifiers=Keys.Shift
  13. KeyPress: KeyChar='A'
  14. KeyDown: KeyCode=Keys.A, KeyData=Keys.A | Keys.Shift, Modifiers=Keys.Shift
  15. KeyPress: KeyChar='A'
  16. KeyUp: KeyCode=Keys.A
  17. 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.

P Daddy