coderef

Keyboard eventsBaseline · Widely available

The keydown and keyup events fire for each key press (or, with modifier keys, a combination of key presses) on a keyboard.

Supported in all major browsers for at least 30 months — safe to use without fallbacks. Baseline since Jul 29, 2015. Widely available since Jan 29, 2018.

Browser support

DesktopChrome
1+Supportedsince Dec 11, 2008
Current: 155
Edge
12+Supportedsince Jul 29, 2015
Current: 154
Firefox
1–5Not supported
6+Supportedsince Aug 16, 2011
Current: 157
Safari
1–1.1Not supported
1.2+Supportedsince Feb 2, 2004
Current: 27
MobileChrome Android
18+Supportedsince Jun 27, 2012
Current: 155
Firefox Android
4–5Not supported
6+Supportedsince Aug 16, 2011
Current: 157
Safari iOS
1+Supportedsince Jun 29, 2007
Current: 27

How to use

This feature is Baseline widely available — use it freely, no fallback needed.

Example

Full syntax and examples are on MDN: keydown event.

Feature detection

JS · check before using
if ("KeyboardEvent" in globalThis) {
  // KeyboardEvent is available
} else {
  // load a polyfill or use a fallback
}

Sub-features

Support for each part of the feature (27). The overall table above shows when the whole feature works.

Sub-featureChromeEdgeFirefoxSafariChrome AndroidFirefox AndroidSafari iOS
Element.keydown_event11261.21861
Element.keyup_event11261.21861
KeyboardEvent1121.51.21841
KeyboardEvent.KeyboardEvent261231726317
KeyboardEvent.altKey1121.51.21841
KeyboardEvent.code48793810.1✕✕10.3
KeyboardEvent.ctrlKey1121.51.21841
KeyboardEvent.getModifierState30121510.1301510.3
KeyboardEvent.getModifierState.key_parameter_alt30121510.1301510.3
KeyboardEvent.getModifierState.key_parameter_altgraph48791510.1481510.3
KeyboardEvent.getModifierState.key_parameter_capslock48121510.1481510.3
KeyboardEvent.getModifierState.key_parameter_control30121510.1301510.3
KeyboardEvent.getModifierState.key_parameter_fn487915✕4815✕
KeyboardEvent.getModifierState.key_parameter_meta30791510.1301510.3
KeyboardEvent.getModifierState.key_parameter_numlock487915✕4815✕
KeyboardEvent.getModifierState.key_parameter_scrolllock487915✕4815✕
KeyboardEvent.getModifierState.key_parameter_shift30121510.1301510.3
KeyboardEvent.getModifierState.key_parameter_symbol4879✕✕48✕✕
KeyboardEvent.isComposing567931✕5631✕
KeyboardEvent.key51122310.1512310.3
Show 7 more
Sub-featureChromeEdgeFirefoxSafariChrome AndroidFirefox AndroidSafari iOS
KeyboardEvent.key.dead_key51793710.1513710.3
KeyboardEvent.key.non_printable_keys51122310.1512310.3
KeyboardEvent.key.printable_key51122910.1512910.3
KeyboardEvent.location301215830158
KeyboardEvent.metaKey1121.51.21841
KeyboardEvent.repeat137122810.11372810.3
KeyboardEvent.shiftKey1121.51.21841