The keydown event fires when the user presses a key — and repeats while the key is held. This tutorial covers .on("keydown"), event.which key codes, eventData, .trigger("keydown"), preventDefault for Enter, document-level shortcuts, and the official jQuery API demos.
01
.on()
Bind handler
02
event.which
Key code
03
.trigger()
Fire keydown
04
Focus
Target element
05
document
Global keys
06
Since 1.7
Modern API
Fundamentals
Introduction
Keyboard shortcuts, live search, games, and accessible widgets all start with one question: which key did the user press? The keydown event answers that question every time a key goes down — before the character appears in an input and before the key is released.
The official jQuery API distinguishes the keydownevent (bound with .on("keydown") since 1.7) from the deprecated .keydown()method in older code. To programmatically run keydown handlers, use .trigger("keydown") since jQuery 1.0.
Concept
Understanding the keydown Event
The keydown event is sent to an element when the user presses a key on the keyboard. If the key is kept pressed, the event is sent every time the operating system repeats the key.
It can be attached to any element, but the event is only delivered to the element that has focus. Form elements — <input>, <textarea>, and <select> — are always reasonable targets. For page-wide shortcut keys, attach the handler to document so bubbling brings every key press to your code.
💡
Beginner Tip
To determine which key was pressed, examine event.which. jQuery normalizes this property across browsers so you can reliably compare it to key codes like 13 (Enter) or 27 (Escape).
Foundation
📝 Syntax
The modern jQuery API for the keydown event has two main forms — binding a handler and triggering the event:
.on() and .trigger() return the original jQuery object for chaining.
Returning false from the handler can cancel default actions — same as preventDefault() + stopPropagation().
Cheat Sheet
⚡ Quick Reference
Goal
Code
Bind keydown handler
$("#target").on("keydown", fn)
Read key code
event.which — 13 Enter, 27 Escape
Pass data to handler
$("#field").on("keydown", { max: 10 }, fn)
Block Enter in input
if (event.which === 13) event.preventDefault()
Global shortcut on page
$(document).on("keydown", fn)
Trigger keydown programmatically
$("#target").trigger("keydown")
Remove keydown handlers
$("#target").off("keydown")
Compare
📋 keydown vs keypress vs keyup vs deprecated .keydown()
Four related keyboard concepts — know which fires when, and what each is best for.
keydown event
key down
Every key press including arrows and modifiers — repeats while held; use event.which
keypress event
char
Mainly printable characters — narrower; prefer keydown or input event in new code
keyup event
key up
Fires when key is released — pair with keydown for hold-to-repeat logic
.keydown() method
deprecated
Old binding shorthand — use .on("keydown") and .trigger("keydown") instead
Hands-On
Examples Gallery
Examples 1–2 follow the official jQuery API documentation. Examples 3–5 extend the official counter demo, document shortcuts, and arrow-key navigation. Use the Try-it links to run each snippet in the browser.
📚 Binding & Triggering
Official jQuery demos for keydown handlers and programmatic triggers.
Example 1 — Official Demo: Bind Handler on #target
Alert when a key is pressed while the input has focus.
Click Trigger the handler → trigger("keydown") → alert runs
Same handler as pressing a real key in #target
Useful for testing keyboard logic without simulating OS input
How It Works
.trigger("keydown") synthetically fires the event. Bound handlers run, but no actual key code is set unless you pass a simulated event object.
📈 Key Codes & Shortcuts
Official extended demo plus practical patterns for Enter, Escape, and arrow keys.
Example 3 — Official Demo: Counter, event.which, and Enter preventDefault
Count keydown events, block Enter from submitting, and log the event object — from the jQuery API documentation.
jQuery
var xTriggered = 0;
$( "#target" ).on( "keydown", function( event ) {
if ( event.which == 13 ) {
event.preventDefault();
}
xTriggered++;
var msg = "Handler for `keydown` called " + xTriggered + " time(s).";
$( "#log" ).prepend( "
Each keydown increments counter and logs event.which
Enter (13) → preventDefault stops form submit side effects
Trigger button also increments counter via .trigger("keydown")
How It Works
event.which holds the key code jQuery normalized across browsers. Compare to 13 for Enter and call preventDefault() when you do not want the default action (such as form submission).
Example 4 — Document-Level Escape to Close a Panel
Attach keydown on document so Escape works anywhere on the page — official API pattern for global shortcuts.
Focus list (tabindex="0") → press Down (40) → next item .active
Press Up (38) → previous item
event.data.wrap available from eventData — extend for wrap-around
How It Works
Non-input elements need tabindex="0" to receive keyboard focus. Arrow key codes 38 and 40 move the highlight; preventDefault() stops the page from scrolling.
Applications
🚀 Common Use Cases
Keyboard shortcuts — Ctrl+S save, Escape close modal — bind on document and filter event.which + modifiers.
Enter to submit vs block — allow Enter in single-line search; preventDefault on Enter in multi-line editors.
Games and demos — arrow keys and Space for movement — keydown repeats while held.
Accessible widgets — roving tabindex lists, menus, and tabs driven by arrow keys.
Live search — react on keydown for instant feedback (often paired with debounce).
Debugging — log event.which during development to learn key codes.
🚀 How the keydown Event Flows
1
User presses key
Physical key goes down while an element (or document) has focus — or code calls .trigger("keydown").
press
2
keydown event fires
Handler runs on the focused element. Event object includes which, shiftKey, ctrlKey, and altKey.
keydown
3
Handler decides
Compare event.which, run shortcut logic, or preventDefault() to block Enter/submit/scroll.
which?
4
⌨
Bubble or repeat
Event bubbles to ancestors (e.g. document). If key stays down, OS sends repeated keydown events until keyup.
Important
📝 Notes
Bind with .on("keydown", handler) since jQuery 1.7 — not the deprecated .keydown(handler) method.
Trigger with .trigger("keydown") since jQuery 1.0.
Event is sent only to the element with focus — click or tab into inputs first.
Use event.which for key codes — jQuery normalizes cross-browser differences.
Held keys repeat — throttle or debounce if handlers do heavy work.
For global shortcuts, bind on document — events bubble up from focused children.
For printable text input, consider the native input event or keypress — keydown fires before the character appears.
Use .off("keydown") to remove handlers — avoid deprecated .unbind("keydown").
Compatibility
Browser Support
The keydown event is a standard DOM keyboard event supported in every browser jQuery targets. jQuery’s .on("keydown") (since 1.7) and .trigger("keydown") (since 1.0) normalize event.which for consistent key codes.
✓ jQuery 1.7+
jQuery keydown event
Supported in jQuery 1.x, 2.x, and 3.x across all modern browsers and IE with supported jQuery versions. Native equivalent: element.addEventListener('keydown', fn) — jQuery adds collection binding, eventData, delegation, and .trigger() in one chainable API.
100%With jQuery loaded
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
keydownUniversal
Bottom line: Safe in any jQuery project. Prefer .on('keydown') over deprecated .keydown() for binding. Use event.which for key codes.
Wrap Up
Conclusion
The jQuery keydown event is the standard hook for keyboard shortcuts, Enter handling, and arrow-key navigation. Bind handlers with .on("keydown", fn), read event.which for key codes, and fire handlers programmatically with .trigger("keydown"). Cancel unwanted defaults with event.preventDefault().
Remember: keydown requires focus on the target element unless you bind on document. For character-by-character text handling, pair keydown with the input event. For dynamic pages, delegate with $("#app").on("keydown", "input", fn).
Bind with .on("keydown", handler) on inputs or document
Use event.which for reliable key codes across browsers
Call event.preventDefault() when blocking Enter or arrow scroll
Set tabindex="0" on custom focusable widgets
Check event.ctrlKey / shiftKey for modifier shortcuts
❌ Don’t
Use deprecated .keydown(handler) for new code — use .on("keydown")
Expect keydown on elements that never receive focus
Confuse keydown with keypress — keydown covers all keys
Run expensive logic on every repeat — debounce or ignore auto-repeat
Hard-code browser-specific key properties — use event.which
Summary
Key Takeaways
Knowledge Unlocked
Six things to remember about the keydown event
Press, code, shortcut.
6
Core concepts
.on01
.on("keydown")
Bind
API
which02
event.which
Key code
Detect
⚡03
.trigger()
Fire
Programmatic
focus04
Focus
Required
Target
doc05
document
Global
Shortcut
↩06
Repeats
While held
OS
❓ Frequently Asked Questions
The keydown event fires when the user presses a key on the keyboard. If the key is kept pressed, the event repeats at the rate set by the operating system. Bind handlers with .on('keydown', handler) since jQuery 1.7. It can attach to any element, but the browser only sends the event to the element that has focus.
Read event.which — jQuery normalizes this property across browsers. Common codes: 13 Enter, 27 Escape, 32 Space, 37–40 arrow keys, 65–90 letter A–Z (uppercase codes regardless of Shift). Compare event.which to the code you care about inside the handler.
keydown fires for every key including arrows, Shift, Ctrl, and function keys. keypress fires mainly when a key produces a character and is deprecated in modern DOM specs. For shortcuts, games, and Enter/Escape handling, keydown is the reliable choice.
Call $('#target').trigger('keydown') since jQuery 1.0. Bound keydown handlers run. Note that .trigger() does not simulate a real physical key press in the browser — it only runs jQuery-bound handlers.
Attach .on('keydown', handler) to document. Key events bubble up from the focused element unless stopped. Check event.which together with event.ctrlKey, event.shiftKey, or event.altKey for combinations like Ctrl+S.
The target element must have focus. Click inside an input first, or use tabindex on a div to make it focusable. If another handler calls event.stopPropagation(), parent handlers may not run. For page-wide shortcuts, bind on document instead of a hidden div.
Did you know?
jQuery’s $.event.special layer maps keyboard events consistently so event.which works even when older browsers exposed different properties (keyCode, charCode). That is why the official API recommends event.which instead of reading raw browser fields — one number for Enter, Escape, and arrow keys everywhere jQuery runs.