本文介绍了在Mac上,在Java中,对于某些键,不会触发keyPressed事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个很奇怪的问题,但是我只是在JPanel中添加了一个简单的keyListener,可以在keyPressed和keyReleased上打印.通常它可以正常工作,但是在某些键(例如"A")上,如果我在释放之前按住,则不会释放其他任何键.在释放后,直到我按住某些键(例如"D")时,才会按下keyPressed.在那之后,除非我按下坏"键并保持太长时间,否则它会恢复正常.

This is a really weird issue, but I just have a simple keyListener added to a JPanel that prints on keyPressed and on keyReleased. Usually it works fine, but on certain keys like 'A', if I press and hold before releasing, no other keys will fire keyPressed after that release until I press and hold on certain keys like 'D'. After that, it's back to usual unless I press a "bad" key and hold it for too long.

最后一个提示,keyReleased总是正确触发,只是keyPressed失败.

One last note, keyReleased ALWAYS triggers properly, it's just keyPressed which fails.

我已经将代码简化为以下内容,并且仍然可以按上述方式运行:

I've simplified the code to simply the following and it still behaves as described above:

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;

public class Test {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setSize(800, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        frame.addKeyListener(new KeyListener() {

            @Override
            public void keyTyped(KeyEvent e) {

            }

            @Override
            public void keyPressed(KeyEvent e) {
                System.out.println("Pressed");
            }

            @Override
            public void keyReleased(KeyEvent e) {
                System.out.println("Released");
            }

        });
    }
}

代码提取:

JFrame frame = new JFrame("test");
frame.setSize(WIDTH, HEIGHT);
frame.setLocationRelativeTo(null);
frame.setLayout(null);
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame.setResizable(false);

JLayeredPane panel = new JLayeredPane();
panel.setBounds(0, 0, WIDTH, HEIGHT);
panel.setLayout(null);


frame.add(panel);
KeyListener listener = new KeyListener() {

    public void keyTyped(KeyEvent e) {
        System.out.println("typed: "+e.getKeyCode());
    }

    @Override
    public void keyPressed(KeyEvent e) {
        System.out.println("pressed: "+e.getKeyCode());
    }

    @Override
    public void keyReleased(KeyEvent e) {
        System.out.println("released: "+e.getKeyCode());
        System.out.println();
    }

};
panel.addKeyListener(listener);
frame.addKeyListener(listener);
frame.setVisible(true);

推荐答案

我最近遇到了此问题,这是由MacOS在按住某些键时显示上下文菜单引起的(以允许您选择其他语言字符)并且错误报告页面提供了一个很好的解决方案,对我有用:

I ran into this issue recently, it is caused by the MacOS showing a context menu when you hold certain keys down (To allow you to chose alternative language characters) and the bug report page had a good solution that worked for me:

https://bugs.java.com/bugdatabase/view_bug.do?bug_id = JDK-8167263

defaults write -g ApplePressAndHoldEnabled -bool false

这可以通过以下方法撤销:

This can be reversed with the following:

defaults write -g ApplePressAndHoldEnabled -bool true

我刚刚在Mac控制台上尝试了此操作,而我的Java应用程序不再遇到关键问题.

I just tried this on the mac console and my java application no longer has the key problem.

这篇关于在Mac上,在Java中,对于某些键,不会触发keyPressed事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 03:27