问题描述
我发现了很多相关的问题(这里和其他地方),但没有具体找到这个问题。
我正在试听keydown箭头键事件(37-40),但是当按照某个顺序使用箭头键时,后续箭头不会生成keydown事件。
示例:
- 在该页面点击type
- 按住右箭头键:表格更新为键码39
- 在继续按住右箭头键的同时,按按住箭头键:表格更新为38
- 继续按住右箭头键和上箭头键,按住
向左箭头键:table 不 update
然而,如果我使用向下箭头键而不是 up 箭头键,然后按预期工作。
另外,如果我使用小键盘而不是箭头键,它可以按预期工作。
我阻止keydown事件的正常运行(通过在事件监听器中返回false并调用preventDefault()),但行为仍然存在。 b
$ b
我认为这可能是我的键盘,但它发生在笔记本电脑和朋友的机器上。
有没有人了解到底是怎么回事?或者一些关于解决方法的好主意?
下面是我的意思。我意识到这可能不适用于所有浏览器,但我只是将它们放在我的笔记本电脑上,以演示我正在发生的事情(在w7上的chrome上,以及在mac os上的chrome& safari上)10.6.8。
$ b
< html>
< body>
< script>
var keysDown = {};
addEventListener(keydown,function(e){
keysDown [e.keyCode] = true;
document.getElementById('latestKeydown')。value = e.keyCode;
},false);
addEventListener(keyup,function(e){
delete keysDown [e.keyCode];
},false);
var loop = function(){
document.getElementById('upinput')。value = keysDown [38];
document.getElementById('downinput')。value = keysDown [40];
document.getElementById('leftinput')。value = keysDown [37];
document.getElementById('rightinput')。value = keysDown [39];
}
setInterval(loop,1);
< / script>
Up:< input id =upinputtype = text size = 10>< br />
Down:< input id =downinputtype = text size = 10>< br />
Left:< input id =leftinputtype = text size = 10>< br />
右:< input id =rightinputtype = text size = 10>< br />
最近的Keydown:< input id =latestKeydowntype = text size = 10>< br />
< / body>
< / html>
问题是:
如果我按住A,那么S,然后D ,然后是F,然后是G,你可以在每次更新时看到最近的Keydown更新,我开始持有一个新的键。
I在这方面不能说权威,但根据,除非您正在同时按下修改键和非修饰键,对于多键按下由于键盘接线方式,PC可能无法理解。
I've found a lot of related questions (here and elsewhere), but haven't found this one specifically.
I'm trying to listen for keydown events for the arrow keys (37-40), however when using the arrow keys in a certain order, subsequent arrow do not generate "keydown" events.
Example:http://blog.pothoven.net/2008/05/keydown-vs-keypress-in-javascript.html
- At that page, click in the "type here ->" box.
- Press and hold right arrow key: table updates to keycode 39
- While continuing to hold right arrow key, press and hold up arrow key: table updates to 38
- While continuing to hold right and up arrow keys, press and holdleft arrow key: table does not update
However, if I do the same thing but using down arrow key instead of up arrow key then it works as expected.
Additionally, if I use the keypad instead of the arrow keys it works as expected.
I am preventing the normal operation of the keydown event (both by returning false in the event listener, and by calling preventDefault() ), but the behavior persists.
I thought it might be my keyboard, but it happens on a laptop as well as a friend's machine.
Does anyone have any insight as to what is going? Or some good ideas on workarounds?
[edit]Here's an example of what I mean. I realize this may not work on all browsers, but I just threw this together on my laptop to demonstrate what is happening for me (on chrome on w7 and also chrome & safari on mac os 10.6.8)
<html>
<body>
<script>
var keysDown = {};
addEventListener("keydown", function(e) {
keysDown[e.keyCode] = true;
document.getElementById('latestKeydown').value=e.keyCode;
}, false);
addEventListener("keyup",function(e){
delete keysDown[e.keyCode];
}, false);
var loop = function(){
document.getElementById('upinput').value=keysDown[38];
document.getElementById('downinput').value=keysDown[40];
document.getElementById('leftinput').value=keysDown[37];
document.getElementById('rightinput').value=keysDown[39];
}
setInterval(loop,1);
</script>
Up: <input id="upinput" type=text size=10><br />
Down: <input id="downinput" type=text size=10><br />
Left: <input id="leftinput" type=text size=10><br />
Right: <input id="rightinput" type=text size=10><br />
Recent Keydown: <input id="latestKeydown" type=text size=10><br />
</body>
</html>
Again, the issue is:If I hold down A, then S, then D, then F, then G, you can see the "Recent Keydown" updating every single time I begin to hold a new key.
However, if I hold down right arrow, then up arrow, then left arrow, I do not see "Recent Keydown" updating for the left arrow key.
I can't speak on authority on this, but according to https://stackoverflow.com/a/4177260/562209 , unless you are working with simultaneous press of a modifier key(s) and a non-modifier key, regarding a multiple key press "the PC might not understand it due to the way keyboards are wired".
这篇关于Javascript:Keydown事件:“向上”箭头键防止进一步箭头键Keydown事件? (回答:键盘重影)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!