How can I detect if the up/down/left/right or the tab key is pressed, I tried to use the code below but it doesn't print something when those keys are pressed
document.addEventListener('keypress', (e) => {
console.log(e.key)
})
You need to use keydown instead of keypress
document.addEventListener("DOMContentLoaded", function(event) {
document.addEventListener('keydown', (e) => {
console.log(e.keyCode)
})
})
document.onkeydown = checkKey;
function checkKey(e) {
e = e || window.event;
if (e.keyCode == '38') {
console.log("up arrow")// up arrow
}
else if (e.keyCode == '40') {
console.log("down arrow")// down arrow
}
else if (e.keyCode == '9') {
console.log("tab")// tab
}
}