keyboard events js code example
Example 1: javascript keyboard events
document.addEventListener("keydown", function(event) {
if (event.keyCode == 32) {
console.log("Space was pressed!");
}
});
Example 2: keyboard event js
document.addEventListener("keydown", function(event) {
console.log(`Key pressed: ${event.key}`);
// event.key is a string representing which key has been pressed
});