jQuery .keydown() Method

Beginner
⚠️ Deprecated since 3.3
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Keyboard events

What You’ll Learn

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

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.

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.

📝 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:

1. Bind a handler — .keydown( handler ) (since 1.0, deprecated 3.3)

jQuery
.keydown( handler )
  • handler — function executed each time keydown fires: function( event ) { ... }.
  • Returns the jQuery object for chaining.
  • Modern replacement: .on( "keydown", handler ).

2. Bind with eventData — .keydown( [eventData], handler ) (since 1.4.3, deprecated 3.3)

jQuery
.keydown( [eventData], handler )
  • eventData — optional object available in the handler as event.data.
  • Modern replacement: .on( "keydown", eventData, handler ).

3. Trigger the event — .keydown() (since 1.0)

jQuery
.keydown()
  • No arguments — triggers keydown on every matched element.
  • Runs bound keydown handlers.
  • Preferred replacement: .trigger( "keydown" ).

Official jQuery API migration note

jQuery
// Deprecated binding
$( "#target" ).keydown( function() {
  alert( "Handler for `keydown` called." );
} );

// Modern binding
$( "#target" ).on( "keydown", function() {
  alert( "Handler for `keydown` called." );
} );

// Deprecated trigger
$( "#target" ).keydown();

// Modern trigger
$( "#target" ).trigger( "keydown" );

Return value

  • All .keydown() signatures return the original jQuery object for chaining.
  • Handler return values are ignored unless you use return false (equivalent to preventDefault() + stopPropagation()).

⚡ Quick Reference

GoalLegacy (.keydown)Modern replacement
Bind keydown handler$("#target").keydown(fn)$("#target").on("keydown", fn)
Pass data to handler$("#search").keydown({ fieldId: "search" }, fn)$("#search").on("keydown", { fieldId: "search" }, fn)
Trigger keydown programmatically$("#target").keydown()$("#target").trigger("keydown")
Trigger on matched elements$("input").keydown()$("input").trigger("keydown")
Remove keydown handlers$("#target").off("keydown")$("#target").off("keydown")
Delegated keydown on inputs$("#app").on("keydown", "input", fn) — not available via .keydown()
Read key codeevent.which — e.g. 13 Enter, 27 Escape

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

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.

jQuery
$( "#target" ).keydown( function() {
  alert( "Handler for \`keydown\` called." );
} );
Try It Yourself

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.

jQuery
$( "#search" ).keydown( { fieldId: "search" }, function( event ) {
  $( "#out" ).text(
    "Field: " + event.data.fieldId + " | key code: " + event.which
  );
} );
Try It Yourself

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.

jQuery
var count = 0;

$( "#target" ).keydown( function( event ) {
  count++;
  $( "#out" ).text( "Target keydown handler fired (count: " + count + ")" );
} );

$( "#fire" ).click( function() {
  $( "#target" ).keydown();
} );
Try It Yourself

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." );
} );
Try It Yourself

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.

jQuery
$( "#target" )
  .keydown( function( event ) {
    if ( event.which === 13 ) {
      event.preventDefault();
    }
    $( "#notice" ).text( "First handler: Enter blocked." ).show();
  } )
  .keydown( function( event ) {
    $( "#result" ).text( "Second handler: key code " + event.which );
  } );
Try It Yourself

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

🚀 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").
  • Shared eventData$("#field").keydown({ id: "search" }, fn) passes metadata without closures.
  • Game and demo code — chained .keydown(fn) calls for movement and action keys.
  • Maintaining legacy admin UIs — many jQuery 1.x panels still use .keydown() — know how to migrate safely.

🧠 How .keydown() Routes Through jQuery

1

You call .keydown()

With a function argument, jQuery treats it as a bind request. With no arguments, it treats it as a trigger request.

dispatch
2

Bind path

.keydown(handler) delegates internally to .on("keydown", handler) — the handler is stored in jQuery’s event system.

.on("keydown")
3

Trigger path

No-arg .keydown() delegates to .trigger("keydown") — bound handlers run when keydown fires.

.trigger("keydown")
4

jQuery object returned

Both paths return the original collection for chaining — .keydown(fn).addClass("watched") works the same as .on("keydown", fn).addClass("watched").

📝 Notes

  • Deprecated since jQuery 3.3 — do not use .keydown(handler) or .keydown(eventData, handler) in new code. Use .on("keydown") instead.
  • Bind with .keydown(handler) since jQuery 1.0; eventData form since 1.4.3.
  • No-argument .keydown() triggers the event since jQuery 1.0 — prefer .trigger("keydown").
  • .keydown() does not support event delegation — use .on("keydown", "input", fn) for dynamic forms.
  • Keyboard events require focus on the target element — click or tab into inputs first.
  • Read key codes with event.which inside handlers — jQuery normalizes cross-browser differences.
  • 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.

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 Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All 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.

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.

💡 Best Practices

✅ Do

  • Use .on("keydown", handler) for all new keydown bindings
  • Use .trigger("keydown") instead of no-arg .keydown()
  • Migrate .keydown(fn) to .on("keydown", fn) when refactoring legacy files
  • Use .off("keydown") to remove handlers cleanly
  • Read the modern keydown event tutorial for preventDefault and AJAX patterns

❌ Don’t

  • 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

Key Takeaways

Knowledge Unlocked

Six things to remember about .keydown()

Legacy bind, modern migrate.

6
Core concepts
data 02

eventData

Since 1.4.3

Handler
() 03

.keydown()

Trigger

No args
.on 04

.on("keydown")

Modern

Replace
05

.trigger()

Fire

Programmatic
3.3 06

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.

Next: keypress event

Learn how keypress fires for printable characters and how event.which differs from keydown.

keypress event 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.

6 people found this page helpful