jQuery Keyboard Events
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.
.on()
Bind handlers
keydown
Key pressed
event.which
Key codes
document
Global shortcuts
.trigger()
Fire events
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.
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:
$("#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:
Keyboard Event Tutorial Index
Search by event name or browse by category. Each tutorial includes syntax, try-it examples, and FAQs.
Keyboard Events
6 tutorialsRespond to keyboard input — keydown when a key is pressed, keyup when released, and keypress for printable characters.
| Event | Description | Tutorial |
|---|---|---|
keydown event | Bind 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 event | Bind 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 event | Bind 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
Start with keydown event
Learn .on("keydown"), event.which, and global shortcuts.
6 people found this page helpful
