The onkeydown attribute is an event-handler attribute. It runs JavaScript when the keydown event fires — a key was pressed down. The target is usually the focused element (<input>, <textarea>, buttons, links, and so on).
Unlike the deprecatedonkeypress event, keydown fires for all keys (including modifiers and navigation keys). Related: onkeyup (key released) and oninput (value changed).
Baseline · Widely available.
Not deprecated and not experimental. The event bubbles and can reach document / window. Stable since about July 2015.
Discouraged as inline HTML.
MDN discourages HTML event handler attributes. Prefer el.addEventListener("keydown", handler) in production; use inline onkeydown for quick demos and learning.
Syntax
HTML Syntax
syntax.html
<input|textarea|… onkeydown="script">
script is JavaScript that runs when a key goes down. Pass event to read event.key / event.code. Equivalent JS: el.onkeydown = handler or el.addEventListener("keydown", handler).
Values
Attribute Values
The attribute value is always a script. Common patterns:
Pick the right one — start an action on down; finish or cleanup on up.
Prefer input when you only care that the field’s value changed.
Edge Cases & Pitfalls
Most shortcut bugs ignore key repeat, IME composition, or still use keypress.
repeat
Auto-repeat while held
keydown can fire many times while a key is held. Check event.repeat if you want only the first press.
deprecated
keypress is deprecated
Prefer keydown / keyup. See onkeypress only for legacy reading — do not use it in new code.
ime
IME composition
For CJK and other IME input, skip composition keys: if event.isComposing (or legacy keyCode === 229), return early so shortcuts do not fire mid-composition.
focus
Needs a keyboard target
If nothing suitable is focused, the target may be body / the root. Put handlers on the control you care about, or listen on document for global shortcuts carefully.
a11y
Do not block needed keys
preventDefault() on Tab, arrows, or browser shortcuts can break accessibility. Only cancel defaults you intentionally replace.
Compatibility
Browser Support
onkeydown is an HTML event-handler attribute for keyboard events in the browser. It is not a Node.js, Deno, or Bun API.
✓ Baseline · Widely available
onkeydown
Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support keydown with event.key and event.code on modern engines.
97%Global support (approx.)
Google ChromeAll modern
Full support
Mozilla FirefoxAll modern
Full support
Apple SafariAll modern
Full support
Microsoft EdgeAll modern
Full support
OperaAll modern
Full support
Samsung InternetModern
Full support
Internet Explorer9+
Supported (prefer key over keyCode)
onkeydownWidely supported
Bottom line: Safe to use for keyboard UX. Prefer addEventListener('keydown'), event.key/code, and skip IME composition for shortcuts.
Remember
Key Takeaways
Role: a key was pressed down (all keys, not only characters).
Use event.key / event.code — not deprecated keyCode.
One line:onkeydown means a key went down — read event.key, watch for repeats, and prefer it over keypress.
Frequently Asked Questions
It is an event-handler attribute. When the user presses a key down while the element can receive keyboard input (usually while focused), the keydown event fires and the JavaScript in onkeydown runs.
Focusable / keyboard-interactive elements such as input, textarea, select, button, links, contenteditable hosts, and elements with an appropriate tabindex. The event bubbles toward document and window.
Use the KeyboardEvent: event.key for the key value (for example a, Enter, Escape) and event.code for the physical key (KeyA, Enter). Prefer these over the deprecated keyCode.
onkeydown fires when the key goes down (and may repeat while held). onkeyup fires when the key is released.
Use keydown (or keyup). The keypress event is deprecated. Per MDN, keydown fires for all keys, not only character-producing keys.
No. keydown and onkeydown are standard and Baseline widely available. Inline HTML handlers are discouraged in production in favor of addEventListener. (keypress is the deprecated sibling.)