使用JavaScript键码-一次按下多个键
我从用户B1KMusic找到了此解决方案: 守则:使用JavaScript键码-一次按下多个键,javascript,key,dom-events,keypress,Javascript,Key,Dom Events,Keypress,我从用户B1KMusic找到了此解决方案: 守则: 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 conditiona
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')
}
这是可行的,但我有一个问题:在按下CTRL+SHIFT+B后收到警报后,我按下任意键,我再次收到警报
B1KMusic发现此工作运行:
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
我尝试了这个方法,但问题仍然存在。事件对象中有ctrl和shift