I'm trying to implement different functions when the up or down arrow key is pressed. Here's what I have so far:
window.addEventListener('keypress', function(e) {
console.log("Key Pressed");
/*Assigns "key" variable to hold the keycode of the key pressed*/
var key = e.which || e.keyCode;
/*Checks if the pressed key is the "up" key*/
if (key === 38){
functionOne();
console.log("Up key was Pressed");
}
/*Checks if the pressed key is the "down" key*/
else if (key === 40){
functionTwo();
console.log("Down key was Pressed");
}
})
The console log message "A key has been pressed" appears whenever any key is pressed other than the arrow keys, shift, alt, caps, tab, and f1 - f12. What might be causing this issue?
Thank you in advance.