- Home
- /
- Tutorials
- /
- JavaScript Tutorial
- /
- JavaScript Keyboard Events
JavaScript Events
JavaScript Keyboard Events
Keyboard events report which key was pressed and let you respond to it.
keydown fires as the key goes down, keyup as it comes back.
event.key tells you which key it was.
Example
Example
javascript
document.body.innerHTML = '<input id="field">';
document.getElementById("field").addEventListener("keydown", function (event) {
console.log("pressed " + event.key);
});
document.getElementById("field").dispatchEvent(
new KeyboardEvent("keydown", { key: "a" })
);