jQuery keyboard events allow you to detect and respond to user keyboard interactions. These events are commonly used in form validation, search boxes, games, shortcuts, and accessibility features.
keydown() – Fires when a key is pressed downkeyup() – Fires when a key is releasedkeypress() – Fires when a key that produces a character is pressedKeyboard events are attached using jQuery event methods. The callback function receives an event object containing key details.
// Basic syntax for keyboard events
$("input").keydown(function(event){
console.log(event.key);
});
// Detect keydown, keyup, and keypress events
$(document).ready(function(){
$("#textBox").keydown(function(){
$("#status").text("Key Down");
});
$("#textBox").keyup(function(){
$("#status").text("Key Up");
});
$("#textBox").keypress(function(){
$("#status").text("Key Pressed");
});
});
When the user types inside the input field, different keyboard events are triggered. Each event updates the text dynamically, showing which keyboard action occurred.
Waiting for input...
Click inside the box area below to activate it, then use your Arrow Keys to move the blue square around. This demonstrates handling specific keys (ArrowUp, ArrowDown, etc.).
Click here & press Arrows
keydown and keyup over keypressevent.key and event.keyCode