HTML onkeydown Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

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 deprecated onkeypress 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.

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).

Attribute Values

The attribute value is always a script. Common patterns:

Value / patternDescriptionTry it
scriptAny JavaScript run when a key is pressed downTry it
onkeydown="fn(event)"Named handler; read event.key / event.codeTry it
addEventListener("keydown")Preferred modern listener — e.g. Enter shortcutTry it
With keyupCompare press vs release timingTry it

Live Preview

Focus the field and press keys. Status shows event.key and event.code on each keydown.

Field

Try letters, Enter, Escape, and Arrow keys. Holding a key may repeat keydown.

Preview

Waiting for keydown…

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Show key and code

Log event.key and event.code when a key goes down.

HTML
<label for="box">Focus, then press a key</label>
<input id="box" type="text" onkeydown="onKey(event)">
<p id="out">key: — &middot; code: —</p>

<script>
  function onKey(event) {
    document.getElementById("out").textContent =
      "key: " + event.key + " · code: " + event.code;
  }
</script>
Try It Yourself

How It Works

  1. Focus the control so it receives keyboard events.
  2. Press a key — keydown fires (may repeat if held).
  3. event.key is the key value (a, Enter, …).
  4. event.code is the physical key (KeyA, Enter, …).

Example 2 — Run an Action on Enter

Detect Enter and optionally stop the default form submit.

HTML
<label for="q">Search</label>
<input id="q" type="search" name="q">
<p id="msg">Press Enter in the field.</p>

<script>
  var q = document.getElementById("q");
  var msg = document.getElementById("msg");

  q.addEventListener("keydown", function (event) {
    if (event.key === "Enter") {
      event.preventDefault();
      msg.textContent = "Enter — searching for: " + q.value;
    }
  });
</script>
Try It Yourself

How It Works

  1. Listen for keydown with addEventListener.
  2. Compare event.key === "Enter" (readable and locale-friendly for named keys).
  3. preventDefault() stops the browser’s default (for example submitting a form).
  4. Use for shortcuts — Escape to close, arrows to move, etc.

Example 3 — keydown vs keyup

See press and release as separate events.

HTML
<label for="box">Hold a key, then release</label>
<input id="box" type="text">
<p id="log">Waiting…</p>

<script>
  var box = document.getElementById("box");
  var log = document.getElementById("log");

  box.addEventListener("keydown", function (event) {
    log.textContent = "keydown: " + event.key;
  });

  box.addEventListener("keyup", function (event) {
    log.textContent = "keyup: " + event.key;
  });
</script>
Try It Yourself

How It Works

  1. keydown runs when the key goes down (and can auto-repeat).
  2. keyup runs once when the key is released.
  3. Pick the right one — start an action on down; finish or cleanup on up.
  4. 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.

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 Chrome All modern
Full support
Mozilla Firefox All modern
Full support
Apple Safari All modern
Full support
Microsoft Edge All modern
Full support
Opera All modern
Full support
Samsung Internet Modern
Full support
Internet Explorer 9+
Supported (prefer key over keyCode)
onkeydown Widely supported

Bottom line: Safe to use for keyboard UX. Prefer addEventListener('keydown'), event.key/code, and skip IME composition for shortcuts.

Key Takeaways

  • Role: a key was pressed down (all keys, not only characters).
  • Use event.key / event.code — not deprecated keyCode.
  • Bubbles — can be handled on parents or document.
  • Avoid keypress — deprecated; prefer keydown/keyup.

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.)

Next: onkeypress

Learn why keypress is deprecated and what to use instead.

onkeypress tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful