Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8167263

[macos] Java keyPressed function of Key Listener doesn't work for W, A, S, D

    XMLWordPrintable

    Details

    • Subcomponent:
    • CPU:
      x86
    • OS:
      os_x

      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.

        Attachments

        1. Test.java
          1 kB
          Pardeep Sharma

          Issue Links

            Activity

              People

              Assignee:
              achung Alisen Chung
              Reporter:
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              8 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: