Reputation: 21
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.
One last note, keyReleased ALWAYS triggers properly, it's just keyPressed which fails.
EDIT2: 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");
}
});
}
}
EDIT: code extract:
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);
Upvotes: 2
Views: 2293
Reputation: 506
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
To disabled the character accent menu and enable auto-repeat, type the following at the command prompt:
defaults write -g ApplePressAndHoldEnabled -bool false
This can be reversed with the following:
defaults write -g ApplePressAndHoldEnabled -bool true
I just tried this on the mac console and my java application no longer has the key problem.
Upvotes: 1
Reputation: 1
More troublesome keys were reported here
https://community.oracle.com/thread/4115318
It seems at this moment the best workaround is to avoid the use of
z, c, n, a, s, e, y, u, i, o
Upvotes: 0
Reputation:
It appears that this is a Java bug that arose after people upgraded to MacOS Sierra:
As a temporary workaround you might try using keys other than the standard WASD for controlling movement.
Upvotes: 0
Reputation: 21
I fixed it by going into my system preferences -> keyboard and moving the key repeat slider all the way to the left to "Off".
Upvotes: 0