HTML global attributeGlobal attributes

onkeydown

Runs JavaScript code the moment a key is pressed. Used for keyboard shortcuts and for handling keys such as Enter or Escape.

Applies to
Elements that can receive focus, and <body>. The event bubbles up the tree
On this page

Example

#
HTML
<label for="keys">Click the field and press any key</label>
<input id="keys" type="text" onkeydown="
  document.getElementById('key').textContent = 'key: ' + event.key + ', code: ' + event.code;
">
<p id="key">Nothing pressed yet.</p>
Preview

Compare a letter with and without Shift, the arrows, Escape, and a letter typed with AltGr, for example é on the US International layout (AltGr+E). event.key changes, while event.code stays KeyE, the physical key.

Definition and usage

#

The onkeydown attribute reacts to the keydown event, which fires the moment a key is pressed, before the character appears in the field. When a key is held down, the event repeats, and event.repeat is then true. Releasing the key is a separate event, keyup.

Keyboard events go to the element that has focus: a form field, a button, a link, or an element with tabindex or contenteditable. An ordinary <div> without focus will not receive them. The event bubbles up the tree, so an attribute on <body> handles keys pressed anywhere on the page.

event.key tells you which key was pressed: a character ("a", "é", "?") or a key name ("Enter", "Escape", "ArrowUp"). event.code, in turn, gives the physical position of the key regardless of the keyboard layout, for example "KeyW" in a game controlled with the WASD keys. The older keyCode property and the keypress event are deprecated. In larger projects, add the handling through addEventListener("keydown", …).

Syntax

#
Syntax
<element onkeydown="JavaScript code">…</element>

Available in the attribute’s code

#
  • event.key

    A character or key name, depending on the keyboard layout and Shift: "a", "A", "Enter", "Escape", "ArrowUp".
  • event.code

    The physical key, regardless of the layout: "KeyA", "Digit1", "Space", "ArrowUp".
  • event.repeat

    true when the event repeats because the key is held down.
  • event.ctrlKey, shiftKey, altKey, metaKey

    Whether Ctrl, Shift, Alt or Cmd on a Mac (the Windows key on a PC) was held along with the key. They let you handle shortcuts such as Ctrl + S.
  • event.preventDefault()

    Blocks the key’s default behavior, for example typing a character into a field or scrolling the page with an arrow.

More examples

#
A to-do list handled with the Enter and Escape keys
HTML
<input id="task" type="text" aria-label="New task"
  placeholder="New task, confirm with Enter"
  onkeydown="
    if (event.key === 'Enter' && this.value.trim() !== '') {
      const item = document.createElement('li');
      item.textContent = this.value;
      document.getElementById('tasks').append(item);
      this.value = '';
    }
    if (event.key === 'Escape') this.value = '';
  ">
<ul id="tasks"></ul>
Preview

Enter adds the task to the list, and Escape clears the field. The field is not inside a form, so Enter submits nothing.

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Do not block standard keys such as Tab, the arrows or browser shortcuts without need. People who use a keyboard and screen readers rely on them.
  • Shortcuts with a bare letter, without Ctrl or Alt, are easy to trigger by accident, and they get in the way especially for people who control their computer by voice. Let users turn them off, or use combinations with a modifier key.

Related entries

#

Spotted a mistake or missing an example? Write to us.