Javascript keyup 无法按预期工作,它会在我没有将手指从按钮上移开的情况下执行
我正在尝试用 javascript 创建一个简单的游戏,但我陷入了如何处理按键的困境。
小例子:
function keyUpEvent(event) {
alert(event.keyCode);
}
window.addEventListener("keyup", keyUpEvent, false);
我正在运行 Ubuntu 9.10 并在 Firefox 3.5 和 Chromium 中进行测试。
如果我立即按下并释放按钮,我会收到警报,这是预料之中的,但是当我按住按钮时,我会短暂停顿,然后出现一系列警报窗口,预期结果是我只收到警报当我将手指从按钮上移开时出现窗口。
我认为这与以下事实有关:当我按住文本区域中的按钮时,我会得到一个字符,短暂的停顿,然后是一系列字符:dddddddddddddddd。
我相信可以解决这个问题,或者做得更正确或者其他什么,因为这个游戏例如: http ://bohuco.net/testing/gamequery/pong.html接缝不会受此影响。但我注意到,如果我尝试 jquery keyup 演示( api.jquery.com/keyup/ ),我会遇到同样的问题。
如何实现基本的游戏按键事件处理?
I'm trying to create a simple game in javascript and I'm stuck on how to deal with keys.
Small example:
function keyUpEvent(event) {
alert(event.keyCode);
}
window.addEventListener("keyup", keyUpEvent, false);
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您给出的示例有解决方案 - 为什么不遵循它呢?
它在 keyDown 之后使用一个计时器,并且不直接调用执行该工作的函数。这就是技巧,你必须使用计时器。
这是类似的问题可以给你多一个想法。
The example that you give, have the solution - why not follow it ?
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.
Here is a similar problem that can give you one more idea.