jQuery Keyboard Events

Beginner
⏱️ Hub guide
📚 Updated: Jul 2026
🚀 6 event tutorials

What You’ll Learn

Keyboard events let JavaScript respond when users type, navigate with arrow keys, or press shortcuts. jQuery’s .on() and .off() provide a consistent API. Start with the keydown event — it fires for every key press and repeats while a key is held down.

01

.on()

Bind handlers

02

keydown

Key pressed

03

event.which

Key codes

04

document

Global shortcuts

05

.trigger()

Fire events

06

6 guides

Full index

Introduction

Search boxes, games, modal dialogs, and productivity apps all depend on keyboard input. The browser fires keyboard events on the element that currently has focus — usually an input, textarea, or the document itself for global shortcuts.

What Are jQuery Keyboard Events?

Keyboard events are named strings passed to .on()"keydown", "keyup", and "keypress". The handler receives an event object with normalized properties like event.which (key code), event.shiftKey, and event.ctrlKey. Unlike deprecated shortcuts like .keydown(fn), .on("keydown", fn) is the modern approach.

💡
Beginner Tip

Use .on("keydown", handler) to react when a key is pressed — the handler receives event.which for the key code. Attach to document for page-wide shortcut keys.

📝 Syntax

Minimal keydown binding workflow:

jQuery
$("#search").on("keydown", function(event) {
  if (event.which === 13) { /* Enter */ }
});
$("#target").trigger("keydown");
$(document).on("keydown", function(event) {
  if (event.which === 27) { /* Escape — global */ }
});

👀 keydown vs keypress vs keyup vs deprecated .keydown()

Four related keyboard concepts — pick the right event:

.on("keydown", fn) → fires when key goes down — arrows, Enter, all keys; repeats while held .on("keypress", fn) → mainly printable characters — narrower use in modern apps .on("keyup", fn) → fires when key is released — good for “key held” end detection .keydown(fn) → deprecated shorthand — use .on("keydown") instead

Keyboard Event Tutorial Index

Search by event name or browse by category. Each tutorial includes syntax, try-it examples, and FAQs.

Keyboard Events

6 tutorials

Respond to keyboard input — keydown when a key is pressed, keyup when released, and keypress for printable characters.

EventDescriptionTutorial
keydown eventBind a handler or trigger the keydown event when the user presses a key — repeats while held; use event.which for key codes.Open
.keydown()Deprecated shorthand to bind or trigger keydown — use .on('keydown') and .trigger('keydown') in new code (since 3.3).Open
keypress eventBind a handler or trigger the keypress event when printable keyboard input is registered — event.which holds character codes.Open
.keypress()Deprecated shorthand to bind or trigger keypress — use .on('keypress') and .trigger('keypress') in new code (since 3.3).Open
keyup eventBind a handler or trigger the keyup event when the user releases a key — pair with keydown for hold detection; use event.which for key codes.Open
.keyup()Deprecated shorthand to bind or trigger keyup — use .on('keyup') and .trigger('keyup') in new code (since 3.3).Open

Conclusion

jQuery keyboard events turn key presses into JavaScript callbacks. Start with .on("keydown", handler) on inputs and forms, read event.which for key codes, and attach to document when you need page-wide shortcuts.

❓ Frequently Asked Questions

Keyboard events respond when the user presses or releases keys — keydown when a key goes down, keyup when it comes up, and keypress for printable character input. jQuery binds them with .on() and normalizes event.which for reliable key codes across browsers.
Use $('#input').on('keydown', function(event) { ... }) since jQuery 1.7. The old .keydown(handler) shorthand is deprecated. Read the key with event.which — 13 is Enter, 27 is Escape, 37–40 are arrow keys.
keydown fires for every key press including arrows, Shift, and Ctrl. keypress fires mainly for keys that produce character input and does not repeat the same way on all keys. For shortcuts and game controls, keydown is usually the right choice; for typed text, keypress or input events may fit better.
Bind keydown on document: $(document).on('keydown', function(event) { if (event.which === 83 && event.ctrlKey) { ... } }). Because of event bubbling, key presses on focused inputs bubble up unless you stop them.
The browser sends keyboard events to the element that has focus. Form inputs, textareas, and contenteditable regions receive keydown when focused. To catch keys anywhere on the page, attach the handler to document and filter by event.which and modifier keys.
Read the overview, study the keydown vs keypress comparison, browse the event index, then open the keydown event tutorial for full syntax, five try-it labs, and event-specific FAQs.

Start with keydown event

Learn .on("keydown"), event.which, and global shortcuts.

keydown 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