Widely available Supported across all major browsers. Safe to use in production.

Browser support

Feature Desktop Mobile
Chrome
Edge
Firefox
Safari
Chrome Android
Safari iOS
1
12
1.5
1.2
18
1

The keydown event is fired when a key is pressed.

1
12
6
1.2
18
1

The keyup event is fired when a key is released.

1
12
6
1.2
18
1

The KeyboardEvent.altKey read-only property is a boolean value that indicates if the alt key (Option or ⌥ on macOS) was pressed (true) or not (false) when the event occurred.

1
12
1.5
1.2
18
1

The KeyboardEvent.code property represents a physical key on the keyboard (as opposed to the character generated by pressing the key). In other words, this property returns a value that isn't altered by keyboard layout or the state of the modifier keys.

48
79
38
10.1
48
10.3

The KeyboardEvent.ctrlKey read-only property returns a boolean value that indicates if the control key was pressed (true) or not (false) when the event occurred.

1
12
1.5
1.2
18
1

The KeyboardEvent.getModifierState() method returns the current state of the specified modifier key: true if the modifier is active (that is the modifier key is pressed or locked), otherwise, false.

30
12
15
10.1
30
10.3
getModifierState (key parameter alt)

`"Alt"` as `key` parameter

30
12
15
10.1
30
10.3
getModifierState (key parameter altgraph)

`"AltGraph"` as `key` parameter

48
79
15
10.1
48
10.3
getModifierState (key parameter capslock)

`"CapsLock"` as `key` parameter

48
12
15
10.1
48
10.3
getModifierState (key parameter control)

`"Control"` as `key` parameter

30
12
15
10.1
30
10.3
getModifierState (key parameter fn)

`"Fn"` as `key` parameter

48
79
15
48
getModifierState (key parameter meta)

`"Meta"` as `key` parameter

30
79
15
10.1
30
10.3
getModifierState (key parameter numlock)

`"NumLock"` as `key` parameter

48
79
15
48
getModifierState (key parameter scrolllock)

`"ScrollLock"` as `key` parameter

48
79
15
48
getModifierState (key parameter shift)

`"Shift"` as `key` parameter

30
12
15
10.1
30
10.3
getModifierState (key parameter symbol)

`"Symbol"` as `key` parameter

48
79
48

The KeyboardEvent.isComposing read-only property returns a boolean value indicating if the event is fired within a composition session, i.e., after Element/compositionstartevent and before Element/compositionendevent.

56
79
31
10.1
56
10.3

The KeyboardEvent interface's key read-only property returns the value of the key pressed by the user, taking into consideration the state of modifier keys such as Shift as well as the keyboard locale and layout.

51
12
23
10.1
51
10.3
key (dead key)

Dead key

51
79
37
10.1
51
10.3
key (non printable keys)

Non-printable keys

51
12
23
10.1
51
10.3
key (printable key)

Printable keys

51
12
29
10.1
51
10.3

The KeyboardEvent() constructor creates a new KeyboardEvent object.

26
12
31
7
26
7

The KeyboardEvent.location read-only property returns an unsigned long representing the location of the key on the keyboard or other input device.

30
12
15
8
30
8

The KeyboardEvent.metaKey read-only property returning a boolean value that indicates if the Meta key was pressed (true) or not (false) when the event occurred. Some operating systems may intercept the key so it is never detected.

1
12
1.5
1.2
18
1

The repeat read-only property of the KeyboardEvent interface returns a boolean value that is true if the given key is being held down such that it is automatically repeating.

137
12
28
10.1
137
10.3

The KeyboardEvent.shiftKey read-only property is a boolean value that indicates if the shift key was pressed (true) or not (false) when the event occurred.

1
12
1.5
1.2
18
1
1+Supported (version) Not supported Has note Sub-feature descriptions sourced from MDN Web Docs (CC BY-SA 2.5)
Notes 2 item(s)
Limitation
  • This browser only partially implements this feature
Implementation note
  • The value is always empty.
Notes 2 item(s)
Removed
  • This feature was removed in a later browser version (79)
Implementation note
  • Previously available under a different name: Scroll (12)
Notes 1 item(s)
Implementation note
  • When selecting an autocomplete suggestion, a `keydown` event fires where the event's `key` property is `undefined`. See bug 41425904.
Notes 1 item(s)
Implementation note
  • When selecting an autocomplete suggestion, a `keydown` event fires where the event's `key` property is `undefined`. See bug 41425904.
Notes 1 item(s)
Implementation note
  • Since Firefox 48, the Windows key is no longer treated as a `meta` key.
Notes 4 item(s)
Implementation note
  • Before Chrome 139, on Linux under X11, if multiple keys are held down, a `keydown` event for the most recently pressed key will trigger with `repeat` incorrectly set to `false`. See bug 40940886.
  • On Windows and Linux, if multiple keys are held down, a `keydown` event for the most recently pressed key will trigger with `repeat` incorrectly set to `false`. See bug 40940886.
Limitation
  • This browser only partially implements this feature
Removed
  • This feature was removed in a later browser version (137)
Notes 4 item(s)
Implementation note
  • Before Chrome Android 139, on Linux under X11, if multiple keys are held down, a `keydown` event for the most recently pressed key will trigger with `repeat` incorrectly set to `false`. See bug 40940886.
  • On Windows and Linux, if multiple keys are held down, a `keydown` event for the most recently pressed key will trigger with `repeat` incorrectly set to `false`. See bug 40940886.
Limitation
  • This browser only partially implements this feature
Removed
  • This feature was removed in a later browser version (137)

Syntax

JAVASCRIPT
addEventListener("keydown", (event) => { })

onkeydown = (event) => { }

Use cases

  • Using Keyboard events

    The keydown event is fired when a key is pressed.

Cautions

  • May not be supported in older browsers.

Implementation notes

  • Some APIs require a secure context (HTTPS) or user activation. Check the requirements before use.