The keydown event fires when the user presses a key. If the key stays down, it repeats at the OS key-repeat rate. Bind with .on("keydown") since jQuery 1.7; only the focused element receives it.
Remember
$("#target").on("keydown", handler) // bind (1.7+)
$("#target").trigger("keydown") // fire handlers (1.0+)
event.which → key code
Enter 13 · Esc 27 · Space 32 · arrows 37–40 · A–Z 65–90
keydown → may repeat while held
keyup → once when the key comes up
Focus a field, bind a handler, then read event.which.
Pick a focusable target — usually an <input> or <textarea>.
Bind with $("#target").on("keydown", handler) (jQuery 1.7+).
Read event.which; use preventDefault() when you block Enter or arrows.
For page-wide shortcuts, bind on document (events bubble).
Piece
Required?
Role
Focused element
Yes
Only the focused element receives keydown
handler
Yes
function(event) { … }
eventData
Optional
Object passed as event.data
Return
—
jQuery object (chainable)
Pseudocode
Pseudocode
bind keydown on focused element
when user presses a key:
keydown fires (and may repeat while held)
code = event.which
if code is Enter: preventDefault() // optional
react (shortcut, move, log…)
when user releases:
keyup fires once
.trigger("keydown") → run handlers without a real press
Reference
Cheat Sheet
Keep these facts handy while wiring press handlers.
Focus #target, press a key →
Handler for keydown called.
(Hold → may repeat; arrows and Escape work too)
How It Works
Select the input.$("#target") must be able to receive focus.
Bind keydown..on("keydown", fn) registers the handler (jQuery 1.7+).
Press a key. The handler runs immediately on press.
Hold may repeat. The OS can fire keydown again while the key stays down.
Example 2 — Trigger Keydown from a Button
Fire the bound handler programmatically with .trigger("keydown").
jQuery
$("#target").on("keydown", function () {
console.log("Handler for keydown called.");
});
$("#other").on("click", function () {
$("#target").trigger("keydown");
});
Focus #board, press ← →
Move left
(Hold → may repeat — good for continuous move)
How It Works
Map which codes 37–40 to directions.
Ignore other keys with an early return.
preventDefault stops page scroll from arrow keys.
Hold-repeat can keep moving while the key stays down.
Edge Cases & Pitfalls
Check these before relying on keydown in real projects.
focus
Needs focus
keydown is delivered only to the focused element. Click the field first, or bind on document for page-wide shortcuts.
repeat
Hold-repeat is real
Holding a key can fire keydown many times. Gate with a flag, or use keyup when you want one action per press.
default
Block Enter / arrows early
Call event.preventDefault() on keydown for Enter (13) or arrows when you own those keys — otherwise forms may submit or the page may scroll.
text
Prefer input for value changes
For live search or character counts (including paste), the input event is often better than keydown alone.
shorthand
Avoid .keydown(fn)
The .keydown() method is deprecated since 3.3. Prefer .on("keydown", fn) and .trigger("keydown").
Compatibility
Browser Support
jQuery’s .on("keydown") (since 1.7) and .trigger("keydown") (since 1.0) work wherever jQuery runs. Native keydown is widely supported. Prefer .on("keydown") over deprecated .keydown().
✓ jQuery 1.7+
jQuery keydown event
Works in Chrome, Firefox, Safari, and Edge with jQuery loaded. Use .on('keydown') for binding and .trigger('keydown') to fire handlers. The .keydown() shorthand is deprecated since 3.3.
ExcellentModern browser support
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
keydownExcellent
Bottom line: Safe for production with jQuery. Bind with .on('keydown'), not deprecated .keydown(). Use event.which for key codes.
Analysis
Performance Notes
Approach
Cost
Best for
.on("keydown", fn)
Per press + OS repeat
Shortcuts; held keys
.on("keyup", fn)
Once per release
Validation; “done pressing”
input event
Per value change
Text fields (incl. paste)
Keep keydown handlers light — held keys can fire many times per second. Debounce expensive search or network work.
Remember
Key Takeaways
Role: react when a key is pressed on the focused element.
API: bind with .on("keydown"); fire with .trigger("keydown").
Codes:event.which — Enter 13, Esc 27, arrows 37–40.
Timing: may repeat while held — use keyup when you need one fire per press.
One line:keydown means “the key went down” — use .on("keydown") and event.which for shortcuts and held-key behavior.
Frequently Asked Questions
keydown fires when the user presses a key. If the key is held, it repeats at the OS key-repeat rate. Bind with .on("keydown", handler) (jQuery 1.7+). Only the focused element receives it.
Read event.which — jQuery normalizes it across browsers. Common codes: 13 Enter, 27 Escape, 32 Space, 37–40 arrows, 65–90 letters A–Z.
keydown fires on press and can repeat while held. keyup fires once on release. Prefer keydown for shortcuts and movement; keyup when you react after the press finishes.
keydown fires for every key (arrows, Escape, modifiers). keypress mainly fires for character keys and is legacy/non-standard. Prefer keydown for shortcuts.
Bind .on("keydown", handler) on document. Key events bubble unless stopped. Combine event.which with event.ctrlKey / event.shiftKey / event.altKey.
Prefer .on("keydown", handler). The .keydown() shorthand is deprecated since jQuery 3.3. Fire with .trigger("keydown").