Reputation: 547
in the code below i'm trying to get it to log "success" into the console when I press the "w" button, but for some reason it's not doing anything when I press it. can someone tell me what I'm doing wrong?
var keysDown = {};
var keysUp = {};
window.addEventListener('keydown', function(e) {
keysDown[e.keyCode] = true;
});
window.addEventListener('keyup', function(e) {
delete keysDown[e.keyCode];
keysUp[e.keyCode] = true;
});
if (37 in keysDown || 65 in keysDown) { //left
console.log("success");
}
Upvotes: 0
Views: 1186
Reputation: 5434
window.addEventListener('keyup', function(e) {
console.log(e.keyCode)
if(e.keyCode == 37 || e.keyCode == 65) console.log('yay')
});
http://jsfiddle.net/zackify/anq34vsv/ Just check for the keycode in the event listener function.
Upvotes: 2