Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JavaScript键码-一次按下多个键_Javascript_Key_Dom Events_Keypress - Fatal编程技术网

使用JavaScript键码-一次按下多个键

使用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

我从用户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 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