1

I have found this solution from User B1KMusic: How to detect if multiple keys are pressed at once using JavaScript?

The code:

map={}//I know most use an array, but the use of string indexes in arrays is questionable
onkeydown=onkeyup=function(e){
    e=e||event//to deal with IE
    map[e.keyCode]=e.type=='keydown'?true:false
    /*insert conditional here*/
}

if(map[17]&&map[16]&&map[65]){//CTRL+SHIFT+A
    alert('Control Shift A')
}else if(map[17]&&map[16]&&map[66]){//CTRL+SHIFT+B
    alert('Control Shift B')
}else if(map[17]&&map[16]&&map[67]){//CTRL+SHIFT+C
    alert('Control Shift C')
}

This works, but I have a problem: After receiving the alert after pressing CTRL+SHIFT+B I press any key and I get the alert again.

B1KMusic found this workaorund:

 if(map[17]&&map[16]&&map[65]){//CTRL+SHIFT+A
    alert('Oh noes, a bug!')
}
//When you Press any key after executing this, it will alert again, even though you 
//are clearly NOT pressing CTRL+SHIFT+A
//The fix would look like this:

 if(map[17]&&map[16]&&map[65]){//CTRL+SHIFT+A
    alert('Take that, bug!')
    map={}
}
//The bug no longer happens since the map object is cleared

I tried this and the problem still occurs.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
carsten
  • 11
  • 3

0 Answers0