javascript - Strange occurrences with controlling multiple keys being held down -


with below code, when key pressed keycode pushed keymap array if it's not there , when key let keycode taken out of array. when testing out shoving keymap array console, found strange things.

var keymap = [];  $(window).keydown(function(e) {     if($.inarray(e.keycode,keymap) == -1) {keymap.push(e.keycode);} });  $(window).keyup(function(e) {     for(i = 0;i < keymap.length;i++) {         if(keymap[i] = e.keycode) {keymap.splice(i,1);}     } });  setinterval(function() {console.log(keymap);},100); 
  1. if hold down , d @ same time keycodes present in keymap, if hold down w 3 of keycodes present. when let go of w it's removed array, d though i'm still holding down d.

  2. i found can hold down a, w, , d , they'll put keymap, not put w, a, , s in keymap when hold them down. combination of 2 of these put in, third not.

can tell me what's going on?

your comparison assignment actually. change if(keymap[i] = e.keycode) to

if (keymap[i] == e.keycode) //             ^ 

also, should use local variables:

for(var = 0; … //  ^^^ 

and while shouldn't matter array items supposed unique, need decrease counter variable i after remove item or skip check next 1 otherwise:

keymap.splice(i--,1) //             ^^ 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -