206

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

Pang
  • 9,564
  • 146
  • 81
  • 122
Josh Kodroff
  • 27,301
  • 27
  • 95
  • 148

10 Answers10

351

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
  • 28,912
  • 9
  • 68
  • 92
  • 2
    Excellent explanation, thx, one caveat though, Escape does not trigger KeyPress on Chrome (not sure about the others). – Barney Szabolcs Apr 29 '13 at 16:54
  • 3
    @BarnabasSzabolcs: Chrome runs .NET? – P Daddy Apr 30 '13 at 23:33
  • @PDaddy ups, I've overlooked, I've thought it was javascript :D but with slightly weird style :P nevertheless, it appears the rules are quite similar :DD ... funnily enough this answer helped me a lot anyway... cheers :) – Barney Szabolcs May 01 '13 at 16:41
  • @PDaddy I know that at least Tab doesnt fire KeyDown or KeyUp by default – PsychoData Mar 06 '14 at 17:09
  • @PsychoData: That's because the Tab key changes focus, so it's not processed as input. If you override `ProcessDialogKey` and return false when `keyData` is `Keys.Tab` or `Keys.Shift | Keys.Tab`, then you'll see the Tab key in (On)Key(Down|Press|Up). – P Daddy Mar 07 '14 at 20:32
  • @PDaddy I handled the `PreviewKeyDown` instead. It's working; I'm not going to mess with it anymore – PsychoData Mar 07 '14 at 22:29
  • A bit of an update. KeyboardEvent.keyCode, as well as keypress, are now marked as deprecated. You don't have to use keyCodes, you get the character directly. For example, event.key === 'a'. If shift key is pressed at that time, then e.shiftKey is true for that event as well. – InvisibleGorilla Sep 09 '21 at 08:06
  • @InvisibleGorilla: The question, and this answer, are about WinForms events in .NET. Your comment is relevant to DOM events in JavaScript. – P Daddy Sep 09 '21 at 17:32
80

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

https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.keypress

Pang
  • 9,564
  • 146
  • 81
  • 122
Jon Spokes
  • 2,599
  • 2
  • 18
  • 21
  • 18
    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 Sep 02 '09 at 13:42
  • 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 Sep 02 '09 at 13:46
  • 12
    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 Sep 02 '09 at 13:55
9

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

stevehipwell
  • 56,138
  • 6
  • 44
  • 61
  • You're close, but off about the sequence. – P Daddy Sep 02 '09 at 13:48
  • @P Daddy - I've corrected it, should have double checked before going from memory. – stevehipwell Sep 02 '09 at 14:07
  • @Stevo3000, fyi: non-printable unicode characters will also fire KeyPress events. I have an app that reads data inserted by a barcode reader into a textbox and the reader includes non-printable unicode characters. – Jeff LaFay Feb 07 '13 at 18:35
  • @jlafay - Not according to [MSDN](http://msdn.microsoft.com/en-gb/library/system.windows.forms.control.keypress(v=vs.110).aspx) _The KeyPress event is not raised by noncharacter keys; however, the noncharacter keys do raise the KeyDown and KeyUp events._ – stevehipwell Feb 11 '13 at 08:09
  • @Stevo3000, I'm not sure how the barcode scanner inputs text into a textbox but the KeyPress fires for special characters that it sends. The value starts with a non-printable character and terminates with a non-printable character. So I'm not sure how it's firing if that's true. Maybe I'll take a look at the .Net assembly that contains the events with IL Spy to get a better look at what is actually detected. – Jeff LaFay Feb 11 '13 at 13:53
5

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
  • 12,948
  • 4
  • 40
  • 61
4

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

Put another way, KeyDown + KeyUp = Keypress

Rob Cowell
  • 1,610
  • 3
  • 18
  • 34
  • 3
    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 Sep 02 '09 at 13:35
  • @RobCowell Also, as jlafay pointed out KeyPress isnt at all dependant on KeyDown or KeyUp. – PsychoData Mar 07 '14 at 22:31
4

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.

Community
  • 1
  • 1
Jon B
  • 51,025
  • 31
  • 133
  • 161
  • FYI, KeyDown and KeyUp also have a Handled property that you may set so that isn't a between KeyPress and the other key events. – Jeff LaFay Feb 07 '13 at 18:39
  • FWIW although KeyUp, like KeyDown, gets a KeyEventArgs parameter its `Handled` property is inoperative for KeyUp (`SuppressKeyPress` is also inoperative). – JonP Oct 10 '16 at 09:23
3

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

Pang
  • 9,564
  • 146
  • 81
  • 122
John Boker
  • 82,559
  • 17
  • 97
  • 130
2

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.

1

KEYUP will be captured only once, upon release of the key pressed, regardless of how long will the key be held down, so if you want to capture such press only once, KEYUP is the suitable event to capture.

Michael Haephrati
  • 3,660
  • 1
  • 33
  • 56
  • The `KeyPress` event is raised for every character generated from input, both in response to physical key presses as well as when the character is the result of the autorepeat feature. [How Keyboard Input Works](https://learn.microsoft.com/en-us/dotnet/framework/winforms/how-keyboard-input-works) documents this behavior. – IInspectable Jan 10 '18 at 20:47
  • You are right. I will update my answer. The correct answer is to use KEYUP which will be triggered once, upon release of one or more key hits. – Michael Haephrati Jan 11 '18 at 12:11
  • While no longer wrong, it still doesn't answer the question that was asked. This is not useful. – IInspectable Jan 12 '18 at 09:36
0

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.

  • damn, misread the question and got it into my head he meant javascript –  Sep 02 '09 at 14:03