jQuery Keydown Event

Beginner
6 min read
Updated: Oct 2026
5 examples
Live preview

What Is keydown?

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
NeedUse
On press / hold.on("keydown", fn)
On release.on("keyup", fn)
Printable characters.on("keypress", fn)
Text value changesinput event (prefer for paste too)

How to Use keydown

Focus a field, bind a handler, then read event.which.

  1. Pick a focusable target — usually an <input> or <textarea>.
  2. Bind with $("#target").on("keydown", handler) (jQuery 1.7+).
  3. Read event.which; use preventDefault() when you block Enter or arrows.
  4. For page-wide shortcuts, bind on document (events bubble).
PieceRequired?Role
Focused elementYesOnly the focused element receives keydown
handlerYesfunction(event) { … }
eventDataOptionalObject 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

Cheat Sheet

Keep these facts handy while wiring press handlers.

TopicDetail
Bind$("#el").on("keydown", fn) (1.7+)
Trigger$("#el").trigger("keydown") (1.0+)
Unbind$("#el").off("keydown")
Key codeevent.which
vs keyupkeydown can repeat; keyup fires once
vs keypresskeydown = any key; keypress ≈ characters
Shorthand.keydown() — deprecated; prefer .on

Live Preview

Focus the field and press a key. Hold it — keydown can repeat. Chips show common event.which codes.

Browser demo of jQuery keydown — same idea as the examples below.

Result
$("#field").on("keydown", …)
→ press a key to see event.which

/* may repeat while held */

jQuery Examples

Short, copy-ready snippets from the official API style. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Bind a Keydown Handler

Official style: log when a key is pressed while the input has focus.

jQuery
$("#target").on("keydown", function () {
  console.log("Handler for keydown called.");
});
Try It Yourself

How It Works

  1. Select the input. $("#target") must be able to receive focus.
  2. Bind keydown. .on("keydown", fn) registers the handler (jQuery 1.7+).
  3. Press a key. The handler runs immediately on press.
  4. 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");
});
Try It Yourself

How It Works

  1. Bind first. Same handler as Example 1 on #target.
  2. Click the button. The click handler runs $("#target").trigger("keydown").
  3. Handlers fire. Bound keydown listeners run; this is not a full OS key simulation.
  4. Useful for demos/tests. Pass a fake event if you need a specific which.

Example 3 — Count Presses and Block Enter

Official demo idea: count keydowns, show event.which, and preventDefault on Enter.

jQuery
var count = 0;

$("#target").on("keydown", function (event) {
  if (event.which === 13) {
    event.preventDefault();
  }
  count++;
  $("#log").text(
    "Called " + count + " time(s). Key: " + event.which
  );
});
Try It Yourself

How It Works

  1. Check which. 13 is Enter.
  2. preventDefault. Stops the default Enter action (e.g. form submit).
  3. Count every press. Including Enter — the handler still runs.
  4. Log the code. event.which is normalized by jQuery.

Example 4 — Escape Shortcut on document

Catch Escape anywhere — key events bubble up unless stopped.

jQuery
$(document).on("keydown", function (event) {
  if (event.which === 27) {
    console.log("Escape pressed — close UI");
  }
});
Try It Yourself

How It Works

  1. Bind on document. Bubbling delivers keydowns from the focused element.
  2. Filter Escape. event.which === 27.
  3. Run once per press (and on hold-repeat if the OS repeats Escape).
  4. Unbind with $(document).off("keydown") when the UI closes.

Example 5 — Arrow Keys

Use keydown for movement — arrows fire here (unlike classic keypress).

jQuery
$("#board").on("keydown", function (event) {
  var map = { 37: "left", 38: "up", 39: "right", 40: "down" };
  var dir = map[event.which];
  if (!dir) return;
  event.preventDefault();
  console.log("Move " + dir);
});
Try It Yourself

How It Works

  1. Map which codes 37–40 to directions.
  2. Ignore other keys with an early return.
  3. preventDefault stops page scroll from arrow keys.
  4. 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").

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.

Excellent Modern browser support
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 Excellent

Bottom line: Safe for production with jQuery. Bind with .on('keydown'), not deprecated .keydown(). Use event.which for key codes.

Performance Notes

ApproachCostBest for
.on("keydown", fn)Per press + OS repeatShortcuts; held keys
.on("keyup", fn)Once per releaseValidation; “done pressing”
input eventPer value changeText fields (incl. paste)

Keep keydown handlers light — held keys can fire many times per second. Debounce expensive search or network work.

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

Next: .keydown() method

Learn the deprecated shorthand and how to migrate to .on("keydown").

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

10 people found this page helpful