How do I detect if an arrow key is pressed in JS? I am using HTML, and JavaScript canvas on a project and need to detect arrow keys pressed.
function arrow() { if (e.code == 'ArrowUp') {
} if (e.code == 'ArrowDown') {
} if (e.code == 'ArrowLeft') {
} if (e.code == 'ArrowRight') {
} } document.addEventListener('keydown', arrow)
I used this in one of my projects, works but is not smooth.
How do I detect if an arrow key is pressed in JS?
How do I detect if an arrow key is pressed in JS? I am using HTML, and JavaScript canvas on a project and need to detect arrow keys pressed.
function arrow() {
if (e.code == 'ArrowUp') {
}
if (e.code == 'ArrowDown') {
}
if (e.code == 'ArrowLeft') {
}
if (e.code == 'ArrowRight') {
}
}
document.addEventListener('keydown', arrow)
I used this in one of my projects, works but is not smooth.