JDK-8167263 : [macos] Java keyPressed function of Key Listener doesn't work for W, A, S, D
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 8,9
  • Priority: P3
  • Status: Closed
  • Resolution: Cannot Reproduce
  • OS: os_x
  • CPU: x86
  • Submitted: 2016-09-25
  • Updated: 2021-10-06
  • Resolved: 2021-10-06
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdResolved
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.8.0_101"
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
macOS Sierra version 10.12 (16A323)

EXTRA RELEVANT SYSTEM CONFIGURATION :
I want to let you know that this bug may seem as if it's a hardware problem on my computer, but it's not. I've tried this with many other computers using the same OS version and have gotten the same result.

A DESCRIPTION OF THE PROBLEM :
So far I've only seen this problem occur with the W, A, S, and D keys. This bug didn't happen before macOS Sierra came out. When pressing these keys (W, A, S, and D) (quickly and many times) the java keyListener doesn't register that I've PRESSED these keys after pressing them quickly several times. Once the 'keyPressed(keyEvent e)' function stops working, a little while later you can get to work for 30 more seconds before it completely breaks down. Again, this bug happened after installing macOS Sierra, not after installing java 1.8.0_101. Meaning it used to work for the same version of Java, but stopped working once upgrading the OS. 

I know it may seem as if this problem is due to the hardware on my computer, but trust me it's not. I've tried this with many other computers using the same OS version and have gotten the same bug.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run a java program that implements the KeyListener interface. Once adding the keyPressed function, have it return a specific result when keyEvent W, A, S, or D is pressed.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
You will see that the expected outcome of the keyPressed function will be returned for a short amount of time, then it will stop returning the expected result whenever the key event is pressed quickly and multiple times. However, I've only found this true when the expected key event is of key(s) W, A, S, or D.
ACTUAL -
Once I ran the program, and pressed the keys A, and D (quickly and multiple times) I was expecting a result saying that I had pressed those keys. The result that was returned was exactly what I had expected BUT it only lasted for a short amount of time. After a short amount of time it no longer gave me the expected result when I had pressed those keys quickly and many times.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
There were no Error Message(s)/ Crash Logs

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

//Julian Abhari
//09/24/16
//TestKeyHandler

@SuppressWarnings("serial")
public class TestKeyHandler extends JFrame implements KeyListener {

    JLabel label;

    public TestKeyHandler(String s) {
        super(s);
        JPanel p = new JPanel();
        label = new JLabel("Key Listener!");
        p.add(label);
        add(p);
        addKeyListener(this);
        setSize(200, 100);
        setVisible(true);

    }

    @Override
    public void keyTyped(KeyEvent e) {

        if (e.getKeyCode() == KeyEvent.VK_D) {
            System.out.println("D key typed");
        }
        if (e.getKeyCode() == KeyEvent.VK_A) {
            System.out.println("A key typed");
        }

    }

    @Override
    public void keyPressed(KeyEvent e) {

        if (e.getKeyCode() == KeyEvent.VK_D) {
            System.out.println("D key pressed");
        }
        if (e.getKeyCode() == KeyEvent.VK_A) {
            System.out.println("A key pressed");
        }

    }

    @Override
    public void keyReleased(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_D) {
            System.out.println("D key Released");
        }
        if (e.getKeyCode() == KeyEvent.VK_A) {
            System.out.println("A key Released");
        }
    }

    public static void main(String[] args) {
        new TestKeyHandler("panelName");
  
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
The only temporary method for bypassing this bug that I've found is to only use KeyEvent.VK_LEFT or KeyEvent.VK_RIGHT. Using those key events, you can press the keys many times.


Comments
When running the test even with ApplePressAndHoldEnabled=true, the keyPresses are repeated so I cannot reproduce this bug.
06-10-2021

http://mail.openjdk.java.net/pipermail/awt-dev/2017-December/013435.html Workaround: The problem of missing events is related the macOS feature which offers the character accent menu when a key is held down, instead of auto-repeating. I experienced the same problem described in the bug but with a different set of keys. (In my case Z, X, : and .) When I disabled the character accent menu the problem disappeared and key events auto-repeated as expected. 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 Hopefully this will provide a pointer for your team to work out how to do this programmatically, but until then the workaround can help interested users.
14-12-2017

Update: - On MAC OS X 10.12.4 (Sierra), checked this randomly for other keys as well and confirmed the issue exists there as well. - On MAC Os X 10.11 (El Capitan), when checked for the WASD and other keys could confirm the result as observed earlier for MAC OS X 10.12. Therefore, this does not seems a regression but a continued behavior.
19-05-2017

Checked this on MAC Sierra 10.12.4 and could confirm the issue with JDK 8u and 9 ea. This issue is restricted to MAC OS X. Result: 8: FAIL 8u60: FAIL 8u131: FAIL 9 ea b169: FAIL Steps to reproduce: 1. Run the attached example program (Test.java) 2. Press WASD keys in sequence (W, A, S, D) Note that the both key pressed and released events are getting registered. 3. Now, Press "S" key for long and release. This time the key pressed event is not registered (See output below). Run with 9 ea b169: =================== run: pressed: 65 typed: 0 released: 65 pressed: 87 typed: 0 released: 87 pressed: 83 typed: 0 released: 83 pressed: 68 typed: 0 released: 68 released: 83 ======================
16-05-2017

The feedback from the bug filer, The bug is when pressing W,A,S, or D keys (probably more but I haven't tested). The keyPressed function in the Key Listener will stop registering that a key has been Pressed after pressing key multiple time and quickly.
07-11-2016