👀 Live Preview
Focus the field and press keys — the last key appears below:
Last key: (none)

The onkeydown attribute is an inline event handler that runs JavaScript when the keydown event fires — the moment a key is pressed down on the keyboard. Attach it to focusable elements like <input> and <textarea>, or listen on document / window with addEventListener for page-wide shortcuts. Use event.key to read which key was pressed (e.g. "Enter", "Escape", "a"). Common uses include keyboard shortcuts, game controls, and live key feedback. Prefer addEventListener("keydown", …) in production; avoid blocking alert() on every keystroke.
Inline JS.
Key pressed.
Which key.
Down vs up.
Preferred way.
Ctrl, Escape.
onkeydown AttributeThe primary purpose of onkeydown is to respond to keyboard input as soon as a key goes down — before it is released. That makes it useful for games, editor shortcuts, “press Enter to search” patterns, and showing which key the user hit. Because keydown fires on every physical press (and repeats while a key is held), you can build responsive keyboard-driven UIs.
For detecting text changes in a field, use oninput instead. For detecting a key release, use onkeyup. Keyboard handlers should respect accessibility — do not trap Tab or Escape without a clear reason.
Use onkeydown="myFunction(event)" so your handler can read event.key, event.code, and modifier flags like event.ctrlKey.
Set onkeydown on a focusable element, or assign element.onkeydown:
<input type="text" onkeydown="myFunction(event)">
<script>
function myFunction(e) {
console.log("Key down:", e.key);
}
</script>keydown event fires.document.addEventListener("keydown", …).event to access key, code, repeat, and modifiers.preventDefault() to block default action.event.repeat === true on repeat fires).element.addEventListener("keydown", handler).keydown event bubbles — delegation on a parent works.The onkeydown attribute accepts a string of JavaScript code:
onkeydown="myFunction(event)" — Call a named function with the event.onkeydown="logKey(event)" — Inline handler reference.field.onkeydown = (e) => { … } assigns the handler.document.addEventListener("keydown", (e) => {
if (e.key === "Escape") {
closeDialog();
}
if (e.ctrlKey && e.key === "s") {
e.preventDefault();
saveDocument();
}
});| Action | keydown fires? | Notes |
|---|---|---|
| Press a letter key in focused input | Yes | Before character appears |
| Hold key down | Yes (repeats) | event.repeat is true |
| Press Tab / Shift+Tab | Yes | Do not block without reason |
| Release key only | No | Use onkeyup |
| Paste text (no key down in field) | No | Use oninput |
| Handler attribute | onkeydown | Inline on element |
| Target | Supported? | Notes |
|---|---|---|
<input>, <textarea> | Yes | When focused |
<button>, <select> | Yes | Space / Enter on button |
tabindex="0" on div | Yes | Custom keyboard targets |
document / window | Yes | Via addEventListener |
<div> without tabindex | No | Not focusable — no key events |
onkeydown vs onkeyup vs oninput| Handler | When it fires | Typical use |
|---|---|---|
onkeydown | Key pressed down | Shortcuts, games, immediate response |
onkeyup | Key released | Actions after key lift |
oninput | Value changed | Live search, character count |
Show the pressed key, register with addEventListener, and clear a field with Escape.
Focus the field and press keys — the last key appears below:
Last key: (none)
Log the key when the user presses down in a text field:
<input type="text" onkeydown="logKey(event)">
<p id="keyOut"></p>
<script>
function logKey(e) {
document.getElementById("keyOut").textContent =
"Key pressed: " + e.key;
}
</script>The browser fires keydown when a key goes down while the input is focused. Passing event gives access to key details.
Attach the handler with addEventListener or the property form:
<input type="text" id="dynamicField">
<script>
document.getElementById("dynamicField").addEventListener("keydown", function (e) {
document.getElementById("log").textContent = "Key pressed: " + e.key;
});
</script>Register once at page load. The handler runs for every keydown while the field has focus.
Clear the input when the user presses Escape:
const search = document.getElementById("search");
search.addEventListener("keydown", (e) => {
if (e.key === "Escape") {
search.value = "";
search.blur();
}
});Compare event.key to a string. No need for legacy keyCode unless supporting very old browsers.
preventDefault() on Tab unless building a focused widget with proper trap semantics.Input or document.
Physical key down.
onkeydown runs.
Shortcuts, games, feedback.
The keydown event and onkeydown handler are supported in all modern browsers — Chrome, Firefox, Safari, and Edge.
onkeydown supportAll major browsers fire keydown and honor the onkeydown handler on focusable elements.
Bottom line: Use event.key and addEventListener for reliable keyboard handling across browsers.
event and use event.key for readable key checksaddEventListener("keydown", …) in production codeoninput when you care about value changes, not keys aloneevent.repeat if repeat fires cause problemsalert() on every keydownonkeypress for new projectsonkeydown alone for paste detection — use oninputtabindexThe onkeydown attribute runs JavaScript when a key is pressed down — essential for shortcuts, games, and immediate keyboard feedback.
Pass the event object, prefer addEventListener, and pair with oninput or onkeyup when those events fit better.
onkeydownBookmark these before adding keyboard handlers.
Before key up.
EventWhich key.
APIKeys vs value.
CompareHeld keys.
GotchaA11y first.
A11ykeydown event fires — when a key is pressed down while the element (or document) receives the event.input, textarea, button, select, links, and elements with tabindex. Also document via script.onkeydown fires when the key goes down. onkeyup fires when it is released.event.key (character or name like Enter) or event.code (physical key like KeyA).addEventListener("keydown", handler) is preferred for production. Inline onkeydown works for learning.Practice the onkeydown attribute with key display and Escape-clear examples in the Try It editor.
5 people found this page helpful