Following a tutorial about game development with HTML5. And I can't figure out why this piece of code won't display the alert when I push the up
key on my keyboard.
var canvas = document.getElementById("mainCanvas");
var context = canvas.getContext("2d");
var keys = [];
window.addEventListener("keydown", function(e) {
keys[e.keyCode] = true;
}, false);
window.addEventListener("keyup", function(e) {
delete keys[e.keyCode]
}, false);
if (keys[38]) alert("yep");
The Chrome console clearly shows that keys[38]
is true
and typing if (keys[38]) alert("yep");
in the console displays the alert. I think I am missing a fundamental thing here (or being fundamentally stupid). I will be glad to provide more info in case there is no obvious mistake in the JS code.
Thanks!