Keyboard Events
In this page:
$(selector).keydown(function (event) {
// event.which
});
$(selector).keyup(handler);
$(selector).keypress(handler);
Keydown
The keydown event fires the instant a key is pressed down, before the character (if any) is registered. This makes it the right event to catch non-printing keys like arrow keys or Escape.
Example: Keydown
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<style>
button { padding:6px 14px; margin:4px; background:#2c6aa0; color:white; border:none; border-radius:4px; cursor:pointer; }
#resetBtn { background:white; color:#2c6aa0; border:1px solid #2c6aa0; }
#output { margin-top:10px; padding:8px; border:1px solid #ccc; border-radius:4px; background:#f7f7f7; font-family:monospace; min-height:24px; }
#demo :where(p, div, span, li, h1, h2, h3, h4, td, th, blockquote, label, section, article, aside) { border:1px solid #4a7fb5; background:#eaf2fb; padding:6px 10px; margin:4px 0; border-radius:4px; }
#demo :where(ul, ol) { border:1px dashed #888; background:#fafafa; padding:8px 8px 8px 32px; margin:6px 0; }
#demo :where(table) { border-collapse:collapse; }
#demo :where(td, th) { border-radius:0; }
#demo :where(input, select, textarea) { margin:4px 0; }
</style>
</head>
<body>
<button id="resetBtn">Reset</button>
<section id="demo">
<input id="field">
</section>
<div id="output"></div>
<script>
var original = $("#demo").html();
function init() {
$("#field").on("keydown", function(event) {
$("#output").append($("<div>").text("Key pressed down: " + event.key));
});
}
init();
$("#resetBtn").click(function() {
$("#demo").html(original);
$("#output").empty();
init();
});
</script>
</body>
</html>
Keyup
keyup fires once a pressed key is released, which makes it useful for reacting after a full keystroke has completed rather than while it's still being held. This is the natural event to use when you want to react to the final character typed, rather than every intermediate keystroke.
Example: Keyup
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<style>
button { padding:6px 14px; margin:4px; background:#2c6aa0; color:white; border:none; border-radius:4px; cursor:pointer; }
#resetBtn { background:white; color:#2c6aa0; border:1px solid #2c6aa0; }
#output { margin-top:10px; padding:8px; border:1px solid #ccc; border-radius:4px; background:#f7f7f7; font-family:monospace; min-height:24px; }
#demo :where(p, div, span, li, h1, h2, h3, h4, td, th, blockquote, label, section, article, aside) { border:1px solid #4a7fb5; background:#eaf2fb; padding:6px 10px; margin:4px 0; border-radius:4px; }
#demo :where(ul, ol) { border:1px dashed #888; background:#fafafa; padding:8px 8px 8px 32px; margin:6px 0; }
#demo :where(table) { border-collapse:collapse; }
#demo :where(td, th) { border-radius:0; }
#demo :where(input, select, textarea) { margin:4px 0; }
</style>
</head>
<body>
<button id="resetBtn">Reset</button>
<section id="demo">
<input id="field">
</section>
<div id="output"></div>
<script>
var original = $("#demo").html();
function init() {
$("#field").on("keyup", function(event) {
$("#output").append($("<div>").text("Key released: " + event.key));
});
}
init();
$("#resetBtn").click(function() {
$("#demo").html(original);
$("#output").empty();
init();
});
</script>
</body>
</html>
Keypress
keypress is an older keyboard event that only fired for character-producing keys and is now deprecated in most browsers -- keydown or keyup should be used for all new code instead.
Example: Keypress
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<style>
button { padding:6px 14px; margin:4px; background:#2c6aa0; color:white; border:none; border-radius:4px; cursor:pointer; }
#resetBtn { background:white; color:#2c6aa0; border:1px solid #2c6aa0; }
#output { margin-top:10px; padding:8px; border:1px solid #ccc; border-radius:4px; background:#f7f7f7; font-family:monospace; min-height:24px; }
#demo :where(p, div, span, li, h1, h2, h3, h4, td, th, blockquote, label, section, article, aside) { border:1px solid #4a7fb5; background:#eaf2fb; padding:6px 10px; margin:4px 0; border-radius:4px; }
#demo :where(ul, ol) { border:1px dashed #888; background:#fafafa; padding:8px 8px 8px 32px; margin:6px 0; }
#demo :where(table) { border-collapse:collapse; }
#demo :where(td, th) { border-radius:0; }
#demo :where(input, select, textarea) { margin:4px 0; }
</style>
</head>
<body>
<button id="resetBtn">Reset</button>
<section id="demo">
<input id="field">
</section>
<div id="output"></div>
<script>
var original = $("#demo").html();
function init() {
// keypress is deprecated; keydown/keyup are preferred for new code
$("#field").on("keypress", function(event) {
$("#output").append($("<div>").text("Character key pressed: " + event.key));
});
}
init();
$("#resetBtn").click(function() {
$("#demo").html(original);
$("#output").empty();
init();
});
</script>
</body>
</html>
Enter Key
Reading event.key inside a handler tells you exactly which key triggered the event, like Enter or ArrowUp, letting you write logic that responds to one specific key rather than any keystroke.
Example: Enter Key
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<style>
button { padding:6px 14px; margin:4px; background:#2c6aa0; color:white; border:none; border-radius:4px; cursor:pointer; }
#resetBtn { background:white; color:#2c6aa0; border:1px solid #2c6aa0; }
#output { margin-top:10px; padding:8px; border:1px solid #ccc; border-radius:4px; background:#f7f7f7; font-family:monospace; min-height:24px; }
#demo :where(p, div, span, li, h1, h2, h3, h4, td, th, blockquote, label, section, article, aside) { border:1px solid #4a7fb5; background:#eaf2fb; padding:6px 10px; margin:4px 0; border-radius:4px; }
#demo :where(ul, ol) { border:1px dashed #888; background:#fafafa; padding:8px 8px 8px 32px; margin:6px 0; }
#demo :where(table) { border-collapse:collapse; }
#demo :where(td, th) { border-radius:0; }
#demo :where(input, select, textarea) { margin:4px 0; }
</style>
</head>
<body>
<button id="resetBtn">Reset</button>
<section id="demo">
<input id="field">
</section>
<div id="output"></div>
<script>
var original = $("#demo").html();
function init() {
$("#field").on("keydown", function(event) {
if (event.key === "Enter") {
$("#output").append($("<div>").text("Enter was pressed"));
}
});
}
init();
$("#resetBtn").click(function() {
$("#demo").html(original);
$("#output").empty();
init();
});
</script>
</body>
</html>
Keyboard Shortcuts
Combining event.key checks with modifier flags like event.ctrlKey or event.shiftKey lets you build simple keyboard shortcuts, such as detecting Ctrl+S inside a form. Remembering to call event.preventDefault() is often necessary here too, since some key combinations have default browser behavior you need to override.
Example: Keyboard Shortcuts
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<style>
button { padding:6px 14px; margin:4px; background:#2c6aa0; color:white; border:none; border-radius:4px; cursor:pointer; }
#resetBtn { background:white; color:#2c6aa0; border:1px solid #2c6aa0; }
#output { margin-top:10px; padding:8px; border:1px solid #ccc; border-radius:4px; background:#f7f7f7; font-family:monospace; min-height:24px; }
#demo :where(p, div, span, li, h1, h2, h3, h4, td, th, blockquote, label, section, article, aside) { border:1px solid #4a7fb5; background:#eaf2fb; padding:6px 10px; margin:4px 0; border-radius:4px; }
#demo :where(ul, ol) { border:1px dashed #888; background:#fafafa; padding:8px 8px 8px 32px; margin:6px 0; }
#demo :where(table) { border-collapse:collapse; }
#demo :where(td, th) { border-radius:0; }
#demo :where(input, select, textarea) { margin:4px 0; }
</style>
</head>
<body>
<button id="resetBtn">Reset</button>
<section id="demo">
<input id="field">
</section>
<div id="output"></div>
<script>
var original = $("#demo").html();
function init() {
$("#field").on("keydown", function(event) {
if (event.ctrlKey && event.key === "s") {
event.preventDefault();
$("#output").append($("<div>").text("Ctrl+S shortcut triggered"));
}
});
}
init();
$("#resetBtn").click(function() {
$("#demo").html(original);
$("#output").empty();
init();
});
</script>
</body>
</html>
- Reading
event.keyCodeand comparing to magic numbers, whenevent.keysuch as"Enter"is clearer and current. - Using the deprecated
keypressevent, which does not fire for keys like arrows or Escape. - Reading an input's value in
keydownand expecting the new character to be included, when the value updates only after the key is processed, sokeyuporinputis the right event.
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: