Javascript键盘无法正常运行,它在我没有从按钮上移开手指的情况下执行 [英] Javascript keyup doesn't work as expected, it executes in instances where I have not removed my finger from a button
问题描述
我正在尝试使用javascript创建一个简单的游戏,并且在如何处理键方面陷入困境.
I'm trying to create a simple game in javascript and I'm stuck on how to deal with keys.
小例子:
function keyUpEvent(event) {
alert(event.keyCode);
}
window.addEventListener("keyup", keyUpEvent, false);
我正在运行Ubuntu 9.10,并在Firefox 3.5和Chromium中对其进行测试.
I'm running Ubuntu 9.10 and testing it in Firefox 3.5 and Chromium.
如果我立即按下并释放一个按钮,我会收到警报,这是可以预期的,但是当我按住一个按钮时,我会停顿一会儿,然后出现一系列警报窗口,预期结果是我只会当我从按钮上移开手指时,会显示一个警报窗口.
If I press and release a button instantly I get an alert, which is to be expected, but when I press and hold a button I get a small pause and then a series of alert windows, the expected result is that I only get an alert window when I remove my finger from a button.
我认为这与以下事实有关:例如,当我在文本区域中按住按钮时,我得到一个字符,稍稍停顿,然后得到一系列字符:dddddddddddddddddd.
I reason it has something to do with the fact that when I press and hold a button in a text area for example I get one character, small pause, and then a series of characters: dddddddddddddddd.
例如,自游戏以来,我认为有可能解决这个问题或做得更正确或做其他事情:
I believe it's possible to get around this or do it more right or whatever since this game for example: http://bohuco.net/testing/gamequery/pong.html seams not to be affected by this. But I notice if I try out the jquery keyup demo ( api.jquery.com/keyup/ ) I get the same problem.
如何实现基本的游戏键事件处理?
How can I implement basic game key event handling?
推荐答案
您给出的示例,有解决方案-为什么不遵循它?
The example that you give, have the solution - why not follow it ?
它在keyDown之后使用一个计时器,并且它不调用直接执行该功能的函数.这就是诀窍,必须使用计时器.
Its use a timer after keyDown, and its not call direct the function that do the job. And this is the trick, you must use a timer.
这是 查看全文