detect key pressed javascript code example
Example 1: js when key is pressed
document.addEventListener("keypress", function(event) {
// do stuff
});
Example 2: how to detect a keypress in javascript
window.addEventListener("keydown", (event) => {
console.log(event.key);
});
Example 3: how do i listen to a keypress in javascript
let b = document.getElementById("body"); //making var for body
b.addEventListener("keydown", (evt) => {//when this happens
console.log(evt.keyCode); //log keycode
});