Java-按住键后KeyBinds停止工作 [英] Java - KeyBinds stop working after holding down a key

查看:108
本文介绍了Java-按住键后KeyBinds停止工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

*更新-我从MacBook上获取了项目,并将其移至Windows计算机(相同的精确代码),该代码按预期运行!有谁知道为什么会这样?

*Update - I took my project from my MacBook and moved it to a windows computer (Same exact code) and the code works just as it is supposed to! Does anyone know why this would happen?

这是我在Stack Overflow上遇到的第一个问题,因此,我将尽我最大的努力来进行正确的格式化.

This is my first question here on Stack Overflow so I will try me best to do the correct formatting.

我正在制作一个简单的Pong程序,该程序需要使用键盘来在关键事件(A和Z)上上下移动.当我简单地键入一个键,但是由于某种原因按下键的时间太长时,该键事件就可以完美地工作,这意味着即使键入键也无法注册任何东西,直到我重新启动程序为止

I am making a simple Pong program that needs a paddle to move up and down on key events (A & Z). The key events work perfect when I simply type a key but when I hold down a key for too long for some reason the key events stop working entirely, meaning that even the typing of the keys won't register anything anymore until I restart the program.

我已经搜索了2个多小时,但无济于事.每个看似相似的问题最终都会得到焦点的答案,即问题,并在InputMap中添加JComponent.WHEN_IN_FOCUSED_WINDOW.但是,我已经尝试过了,但是它根本没有帮助或解决我的问题.

I have searched for the answer for over 2 hours with no avail. Every question that seems similar ends up with an answer of the focus being the problem and adding JComponent.WHEN_IN_FOCUSED_WINDOW inside the InputMap. However I have tried this and it does not help or fix my problem at all.

这是我的代码:

public class ColorPanel extends JPanel implements ActionListener{
    public ColorPanel(Color backColor, int width, int height){

   //Non Relavent coding removed

    //Code for KeyBinding

    InputMap im = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    ActionMap am = getActionMap();

    im.put(KeyStroke.getKeyStroke(65, 0, false), "actionName");
    im.put(KeyStroke.getKeyStroke(65, 0, true), "actionName");
    im.put(KeyStroke.getKeyStroke(KeyEvent.VK_Z, 0, true), "actionName");
    im.put(KeyStroke.getKeyStroke(KeyEvent.VK_Z, 0, false), "actionName");

    am.put("actionName", new AbstractAction("actionName") {
          public void actionPerformed(ActionEvent e) {
            //System.out.println("---" + e.getActionCommand() + "---");
            if(e.getActionCommand().equals("a")){
                System.out.println("a!");
            }else if (e.getActionCommand().equals("z")){
                System.out.println("z!");
            }
          }
        });


}

推荐答案

您需要禁用通常在按住某个键时出现的macOS口音弹出窗口.由于某种原因,这使Java感到困惑.参见此处: http://www.techradar.com/how-to/computing/apple/easy-mac-hacks-disable-the-pop-up-accent-window-when-typing-1305618

You need to disable the macOS accent popup that usually appears when you hold down a key. For some reason this is confusing Java. See here: http://www.techradar.com/how-to/computing/apple/easy-mac-hacks-disable-the-pop-up-accent-window-when-typing-1305618

这篇关于Java-按住键后KeyBinds停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆