为什么按下两个键时不发送按键释放事件?

发布于 2024-12-03 19:00:38 字数 796 浏览 0 评论 0 原文

当您按单个键时,以下代码将起作用。

@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”键发送按键释放事件?

The following code works when you press single keys.

@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);

But when you press multiple keys like "A" first then "B", the listener will only receive key up event for "B" and no events for "A". So as a result, ...

switch(evt.type) {
case SWT.KeyDown:
    mKeyMap.get(evt.keyCode).isDown = true;
    break;

case SWT.KeyUp:
    mKeyMap.get(evt.keyCode).isDown = false;
    break;
}

The key "A" will always remain true, until you press it again and receive key down event and key up event. This problem doesn't happen with the arrow keys though. You can press multiple arrow keys and it sends key up events correctly.

So, why isn't key released event sent for key "A" when keys "A" and "B" are pressed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

极度宠爱 2024-12-10 19:00:38

环顾四周(因为我对一些游戏代码也有同样的问题)我发现了这个 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. :(

初雪 2024-12-10 19:00:38

这在一定程度上会受到键盘的影响。不过,我不确定这是否是导致您出现问题的原因,但请查看此链接:

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

誰ツ都不明白 2024-12-10 19:00:38
function isUserPressingCopy(){
    var copy = ["Meta", "c"]
    var map = {};
    let buttonPressed = []
    onkeydown = onkeyup = function(event){
    event;
    map[event.key] = event.type == 'keydown';
    buttonPressed.push(map[event.key])
    if (Object.values(map).every(item => item === true)){
      if(JSON.stringify(Object.keys(map)) == JSON.stringify(copy))
      console.log("you pressed copy")
    }
    else{
      map = {}
    }
  }
}

当您在控制台中声明此函数并执行它时,您应该能够在 mac 上执行“cmd”+“c”,并且控制台会告诉您是否按下了复制(通过将其与我设置的复制变量进行比较)。

这是我检查两个按键是否按下的方法,我必须创建一个存储按下的按键的对象,为预期的按键创建一个对象并以数组形式将它们字符串化以进行比较,就个人而言,这是我搜索多个按键的首选方式事件。

(尝试通过使用热键作为示例来简化此操作,但我希望它有所帮助!)

function isUserPressingCopy(){
    var copy = ["Meta", "c"]
    var map = {};
    let buttonPressed = []
    onkeydown = onkeyup = function(event){
    event;
    map[event.key] = event.type == 'keydown';
    buttonPressed.push(map[event.key])
    if (Object.values(map).every(item => item === true)){
      if(JSON.stringify(Object.keys(map)) == JSON.stringify(copy))
      console.log("you pressed copy")
    }
    else{
      map = {}
    }
  }
}

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!)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文