The .keydown() method is jQuery’s legacy shorthand for binding and triggering the keydown event. This tutorial covers .keydown(handler) since 1.0, .keydown(eventData, handler) since 1.4.3, no-argument .keydown() as a trigger, migration to .on("keydown") and .trigger("keydown"), and why the binding form is deprecated since jQuery 3.3. See the jQuery keydown event tutorial for the modern API.
01
.keydown(fn)
Bind handler
02
eventData
Since 1.4.3
03
.keydown()
Trigger event
04
.on()
Modern bind
05
.trigger()
Modern fire
06
Deprecated
Since 3.3
Fundamentals
Introduction
Before jQuery 1.7 unified events with .on() and .off(), every common event had a matching shorthand — .keydown(), .keyup(), .click(), and more. The .keydown() method let you react to keyboard input in one short call: $("#search").keydown(function(e){ if(e.which===13){ ... } }). It remains in jQuery 3.x for backward compatibility but is marked deprecated.
Understanding .keydown() matters when reading older game, search, and admin-panel tutorials in legacy codebases. The method does two distinct jobs: with a function argument it binds; with no arguments it triggers. Modern jQuery splits those roles explicitly — .on("keydown", handler) for binding and .trigger("keydown") for firing.
⚠️
Focus required
The jQuery .keydown() method attaches to any element, but the browser only delivers keydown events to the element that has focus. Click inside an input or set tabindex="0" on custom widgets before expecting handlers to run. For global shortcuts, use $(document).on("keydown", fn) instead of .keydown() on a hidden div.
Concept
Understanding the .keydown() Method
The official jQuery API describes .keydown() as a way to bind an event handler to the keydown event, or trigger that event on an element. When you pass a handler, jQuery registers it on each matched element and calls it when the user presses a key while that element has focus — the same keydown event that .on("keydown") handles today.
When you call .keydown() with no arguments, jQuery synthetically fires the keydown event on each matched element. Bound handlers run once per call. This trigger behavior is equivalent to .trigger("keydown").
⚠️
Deprecated since jQuery 3.3
Do not use .keydown(handler) or .keydown(eventData, handler) in new code. Replace them with .on("keydown", handler) or .on("keydown", eventData, handler). Replace no-argument .keydown() with .trigger("keydown"). For event.which, global shortcuts, and the full modern keydown guide, read the jQuery keydown event tutorial.
Foundation
📝 Syntax
The .keydown() method has three signatures from the official jQuery API. The binding forms are deprecated; the trigger form remains valid but .trigger("keydown") is preferred:
$("#app").on("keydown", "input", fn) — not available via .keydown()
Read key code
event.which — e.g. 13 Enter, 27 Escape
Compare
📋 .keydown() vs .on("keydown") vs .trigger("keydown") vs .off("keydown")
Four related APIs — know which one binds, which one fires, and which one removes handlers.
.keydown()
deprecated
Legacy bind (.keydown(fn)) or trigger (.keydown()) — use modern APIs for new code
.on("keydown")
bind
Modern binding since 1.7 — supports eventData and delegation on dynamic inputs
.trigger("keydown")
fire
Programmatically run handlers — clearer than no-arg .keydown()
.off("keydown")
remove
Unbind handlers — pair with .on("keydown") when cleaning up or in SPAs
Hands-On
Examples Gallery
Five examples cover binding, eventData, triggering, migration from .keydown() to .on("keydown"), and method chaining. Use the Try-it links to run each snippet in the browser. Remember: binding with .keydown(handler) is deprecated — these demos show legacy syntax you will encounter in older code.
📚 Binding & Triggering
Legacy .keydown() signatures for binding handlers and firing events programmatically.
Example 1 — Bind Handler with .keydown(handler)
The canonical legacy pattern — alert when a key is pressed while the input has focus.
Focus #target input, press any key → alert runs
Repeats while key is held (OS key repeat)
Deprecated — prefer .on("keydown", fn) for new code
How It Works
.keydown(fn) registers the function on the element. jQuery internally routes this to .on("keydown", fn). The handler runs each time a key goes down while the element has focus.
Example 2 — Pass eventData with .keydown(eventData, handler)
Since jQuery 1.4.3, pass field metadata before the handler — it arrives as event.data.
Focus #search, press a key → "Field: search | key code: 65" (for A)
eventData.fieldId passed without a closure
Modern: .on("keydown", { fieldId: "search" }, fn)
How It Works
jQuery stores the { fieldId: "search" } object and injects it into event.data when keydown fires. Modern equivalent: .on("keydown", { fieldId: "search" }, fn).
Example 3 — Trigger Keydown with No-Argument .keydown()
Calling .keydown() without a handler fires the keydown event programmatically — bound handlers run once per call.
Press a key in #target → count increments
Click #fire → $("#target").keydown() → same handler runs
No-arg .keydown() is trigger shorthand — prefer .trigger("keydown")
How It Works
The first .keydown(function(){ ... }) binds a handler on #target. The button calls $("#target").keydown() with no arguments — that synthetically fires keydown. Replace with $("#target").trigger("keydown") for clarity.
📈 Migration & Chaining
Compare legacy and modern APIs, and chain after .keydown(handler).
Example 4 — Migration: .keydown(fn) vs .on("keydown", fn)
Both bind the same keydown event — .on() is the recommended API for new code.
jQuery
$( "#legacy" ).keydown( function() {
$( "#out" ).text( "Legacy: .keydown(function(){ ... }) — deprecated since jQuery 3.3." );
} );
$( "#modern" ).on( "keydown", function() {
$( "#out" ).text( "Modern: .on('keydown', function(){ ... }) — use this for new code." );
} );
Focus #legacy, press a key → legacy deprecation message
Focus #modern, press a key → modern .on("keydown") message
Behavior is identical — only the API name differs
How It Works
Under the hood, .keydown(fn) calls .on("keydown", fn). Migration is a straight rename with no behavior change for simple bindings. The advantage of .on() is delegation — $("#app").on("keydown", "input", fn) — which .keydown() cannot do.
Example 5 — Chaining After .keydown(handler)
.keydown(handler) returns the jQuery object — chain multiple keyboard handlers on the same input.
Press any key in #target
First handler shows #notice; Enter gets preventDefault
Second handler updates #result with event.which
Both handlers run in bind order on each keydown
How It Works
Multiple .keydown(fn) calls on the same element stack handlers — all run when a key is pressed. Because each call returns the jQuery object, you can chain them fluently. The same pattern works with .on("keydown", fn).
Applications
🚀 Common Use Cases
Reading legacy code — recognize $("#search").keydown(fn) as equivalent to .on("keydown", fn) in older scripts.
Enter key handling — legacy tutorials bind .keydown() to block or allow Enter in inputs.
Programmatic trigger — $("#target").keydown() from a test button — replace with .trigger("keydown").
Remove handlers with .off("keydown") — not by calling .keydown(undefined).
For the full modern keydown guide — event.which, global shortcuts, and Enter handling — see the jQuery keydown event tutorial.
Compatibility
Browser Support
The underlying keydown event fires on focused elements in browsers jQuery targets. The .keydown() method itself is a jQuery API — it works in jQuery 1.x, 2.x, and 3.x. Binding via .keydown(handler) is deprecated since 3.3 but still functional; triggering via no-arg .keydown() also remains supported.
✓ jQuery 1.0+
jQuery .keydown() method
Supported across all jQuery versions you are likely to maintain. Deprecated binding since 3.3 — use .on('keydown') for new projects. Native equivalent for binding: element.addEventListener('keydown', fn). Native equivalent for trigger: .trigger('keydown') or dispatchEvent.
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
.keydown()Legacy API
Bottom line: Safe in legacy jQuery projects. Migrate bindings to .on('keydown') and triggers to .trigger('keydown') when updating keyboard shortcut and search code.
Wrap Up
Conclusion
The jQuery .keydown() method is the legacy shorthand for binding and triggering keydown handlers. Use .keydown(handler) or .keydown(eventData, handler) to bind — both deprecated since jQuery 3.3. Use no-argument .keydown() to trigger — equivalent to .trigger("keydown").
For new code, prefer .on("keydown", fn) for binding and .trigger("keydown") for programmatic firing. When you encounter .keydown() in older search, game, and admin scripts, recognize it, understand it, and migrate when practical. For event.which, global shortcuts, and the complete modern keydown workflow, continue with the jQuery keydown event tutorial.
Write new keyboard handlers with deprecated .keydown(handler)
Expect keydown only on focused elements — use document binding for global shortcuts
Assume .keydown() supports delegation — it does not
Confuse .keydown(fn) (bind) with .keydown() (trigger)
Use .unbind("keydown") — it is also deprecated; use .off("keydown")
Copy deprecated patterns from old tutorials without modernizing them
Summary
Key Takeaways
Knowledge Unlocked
Six things to remember about .keydown()
Legacy bind, modern migrate.
6
Core concepts
.kd01
.keydown(fn)
Bind
Deprecated
data02
eventData
Since 1.4.3
Handler
()03
.keydown()
Trigger
No args
.on04
.on("keydown")
Modern
Replace
⚡05
.trigger()
Fire
Programmatic
3.306
Deprecated
Since 3.3
Migrate
❓ Frequently Asked Questions
The .keydown() method has two roles. With a handler argument — .keydown(handler) or .keydown(eventData, handler) — it binds a function that runs when the user presses a key on a matched element that has focus. With no arguments — .keydown() — it triggers the keydown event on each matched element, running bound handlers. Both forms have been part of jQuery since 1.0; the binding form is deprecated since 3.3.
Yes — the binding signatures .keydown(handler) and .keydown(eventData, handler) are deprecated since jQuery 3.3. Use .on('keydown', handler) or .on('keydown', eventData, handler) instead. The no-argument .keydown() trigger form still works but .trigger('keydown') is clearer and preferred in modern code.
.keydown(handler) registers a callback — it does not fire the event immediately. .keydown() with no arguments triggers the keydown event programmatically on every matched element, the same as .trigger('keydown'). The handler form is deprecated for binding; the no-arg form is a trigger shorthand.
Replace .keydown(fn) with .on('keydown', fn). Replace .keydown(data, fn) with .on('keydown', data, fn). Replace no-arg .keydown() with .trigger('keydown'). Replace .unbind('keydown') cleanup with .off('keydown'). Under the hood, .keydown(handler) already delegates to .on('keydown') — migration is a straight rename.
Yes. jQuery 3.x keeps .keydown() for backward compatibility. It still binds and triggers correctly. The API docs mark it deprecated to steer new projects toward .on() and .trigger(). Do not use .keydown(handler) in new code — prefer the modern API in the keydown event tutorial.
No — the browser only sends keydown to the focused element. .keydown(handler) on an input runs when that input has focus. For page-wide shortcuts, bind .on('keydown', fn) on document instead. The shorthand .keydown() does not change this rule.
Did you know?
jQuery event shorthands like .keydown(handler) were thin wrappers around .bind("keydown", handler) before .on() unified the API in jQuery 1.7. When you call .keydown(fn) today, jQuery still routes through the same event system as .on("keydown", fn) — which is why migration is a rename with no behavior change for simple bindings.