The keydown event fires when a key is pressed. Learn addEventListener vs onkeydown, key vs code, modifiers, how it differs from keyup / keypress, IME tips, and five try-it labs.
01
Kind
Instance event
02
Type
KeyboardEvent
03
When
Key pressed down
04
Handler
onkeydown
05
Fields
key / code
06
Status
Baseline widely available
Fundamentals
Introduction
Keyboard shortcuts, games, accessible menus, and Escape-to-close dialogs all start with knowing when a key goes down. The keydown event is that signal.
The event target is usually the focused element (an <input>, contenteditable host, button, and so on). If nothing suitable is focused, it may be Document or the root. The event bubbles.
💡
Beginner tip
Prefer event.key (and sometimes event.code) over legacy keyCode. Prefer keydown over the deprecated keypress event for modern apps.
Concept
Understanding keydown
An instance event that answers: “Did a key just go down (possibly repeating while held)?”
Fires when any key is pressed (including modifiers and arrows).
A KeyboardEvent (inherits from UIEvent and Event).
Handy properties
Property
Meaning
key
Key value (e.g. "a", "Enter", "Escape")
code
Physical key id (e.g. "KeyA", "ArrowUp")
ctrlKey / shiftKey / altKey / metaKey
Modifier flags active during the event
repeat
true when auto-repeat fires while the key is held
isComposing
true during IME composition (between compositionstart/end)
Compare
⚖️ keydown vs keyup vs keypress
Topic
keydown
keyup
keypress
When
Key goes down (may repeat)
Key is released
Character-producing keys (legacy)
All keys?
Yes
Yes
No — mainly printable
Status
Modern / Baseline
Modern / Baseline
Deprecated
Best for
Shortcuts, games, Escape
Release / stop actions
Avoid in new code
Caveat
⚠️ IME Composition & isComposing
During Input Method Editor (IME) composition (common for CJKT text), browsers may fire keydown events that you should ignore for shortcuts. MDN recommends skipping when event.isComposing is true, and also checking the legacy keyCode === 229 marker.
keydown is marked Baseline Widely available on MDN. Logos use the shared browser-image-sprite.png sprite from this project. KeyboardEvent key/code are the modern path across engines.
✓ Baseline · Widely available
Element keydown
Fires when a key is pressed; works for all keys including modifiers and navigation.
FullWidely available
Google ChromeFull support
Yes
Mozilla FirefoxFull support
Yes
Apple SafariFull support
Yes
Microsoft EdgeFull support
Yes
OperaFull support
Yes
Internet ExplorerSupported (prefer feature-detect modern key/code)
Partial
keydownBaseline
Bottom line: Safe default for shortcuts. Guard IME composition and prefer key/code over legacy keyCode alone.
Wrap Up
Conclusion
keydown is the modern foundation for keyboard interaction: detect the press, read key / code, respect modifiers, and ignore IME noise when building shortcuts.
It fires when a key is pressed down. Unlike the deprecated keypress event, keydown fires for all keys — including modifier and navigation keys — whether or not they produce a character.
No. MDN marks Element keydown as Baseline Widely available. It is not Deprecated, Experimental, or Non-standard. (keypress is the deprecated relative.)
A KeyboardEvent (inherits from UIEvent and Event). Useful properties include key, code, altKey, ctrlKey, shiftKey, metaKey, repeat, and isComposing.
key is the value produced (for example 'a' or 'Enter'), considering layout and Shift. code is the physical key position (for example 'KeyA'), which stays the same across layouts.
keydown fires when the key goes down (and may repeat while held). keyup fires when the key is released. Shortcuts often listen on keydown; cleanup can use keyup.
Skip events when event.isComposing is true, and also when event.keyCode === 229 (legacy IME marker). MDN documents this for better CJKT compatibility.
Did you know?
After pressing Tab, the keydown target can differ from the keyup target because focus already moved. Always check event.target if your logic depends on which element is focused.