为什么按下两个键时不发送按键释放事件?
当您按单个键时,以下代码将起作用。
@Override public void handleEvent(Event evt) {
switch(evt.type) {
case SWT.KeyDown:
System.out.println(evt.keyCode + " pressed");
break;
case SWT.KeyUp:
System.out.println(evt.keyCode + " released");
break;
}
}
...
widget.addListener(SWT.KeyDown, this);
widget.addListener(SWT.KeyUp, this);
但是,当您先按“A”然后按“B”等多个键时,侦听器将仅收到“B”的按键事件,而不会收到“A”的事件。因此,...
switch(evt.type) {
case SWT.KeyDown:
mKeyMap.get(evt.keyCode).isDown = true;
break;
case SWT.KeyUp:
mKeyMap.get(evt.keyCode).isDown = false;
break;
}
键“A”将始终保持为真,直到您再次按下它并接收到按键按下事件和按键按下事件。不过,使用箭头键不会出现此问题。您可以按多个箭头键,它会正确发送按键事件。
那么,为什么当按下“A”和“B”键时,没有为“A”键发送按键释放事件?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
环顾四周(因为我对一些游戏代码也有同样的问题)我发现了这个 Eclipse bug:
Bug 50020 - KeyReleased 无法正常工作。
似乎这个问题已经存在了大约 8 年,并且不太可能很快得到解决/修补。 :(
Looking around (as I have the same problem for a spot of game code) I found this Eclipse bug:
Bug 50020 - KeyReleased not working correctly.
Seems like the problem has been around for some 8 years and is not likely to be solved / patched anytime soon. :(
这在一定程度上会受到键盘的影响。不过,我不确定这是否是导致您出现问题的原因,但请查看此链接:
http://www.tomshardware.com/forum/50383-28-pressing-multiple-keys-keyboard-problem
This can be influenced by your keyboard to an extent. I'm not sure if that's what might be causing your problem, though, but take a look at this link:
http://www.tomshardware.com/forum/50383-28-pressing-multiple-keys-keyboard-problem
当您在控制台中声明此函数并执行它时,您应该能够在 mac 上执行“cmd”+“c”,并且控制台会告诉您是否按下了复制(通过将其与我设置的复制变量进行比较)。
这是我检查两个按键是否按下的方法,我必须创建一个存储按下的按键的对象,为预期的按键创建一个对象并以数组形式将它们字符串化以进行比较,就个人而言,这是我搜索多个按键的首选方式事件。
(尝试通过使用热键作为示例来简化此操作,但我希望它有所帮助!)
When you declare this function in your console and execute it, you should be able to do "cmd" + "c" on mac and the console will tell you if you pressed copy (by comparing it to the copy variable I set).
This is my way of checking two keys were pressed pressed, I had to create an object storing the keys pressed, create an object for the expected keys and stringify them in array form for comparison, personally this is my preferred way of searching for multiple keypress events.
(tried to simplify this by using a hotkey as an example but I hope it helps!)