Skip to content
How do I detect if an arrow key is pressed in JS?
Profile icon
JoshuaRogers6

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.

You are viewing a single comment. View All
Profile icon
ZarmDev

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.