0

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");
}
user3150635
  • 509
  • 2
  • 9
  • 26

1 Answers1

2
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.

zackify
  • 5,314
  • 2
  • 22
  • 28