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

Misinterpretation of Key Press Method in Java's AWT Robot Class

XMLWordPrintable

    • generic
    • windows

      ADDITIONAL SYSTEM INFORMATION :
      The operating system I am using is Windows 11 with JDK version 17

      A DESCRIPTION OF THE PROBLEM :
      In the "AWT" package of Java's "JDK", which contains the "Robot" class, there is a method that simulates a key press from the keyboard. However, this simulation is not well designed. When pressing a key using the keyboard, it repeats until the key is released. In contrast, the "keyPress" method of the "Robot" class (at least on my operating system) only shows a single key press. For example, pressing the "a" key for 10 seconds with the keyboard results in "aaaaaaaa", while simulating the same with "keyPress" results in "a". It seems that each call to the method counts as a single "press/character" and then the key is "released", so to speak. This would be fine if it were not for the fact that the same class has a "keyRelease" method. What is the purpose of this method, if each call to "keyPress" counts as a single "press/character" that is then "released" instantly?



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Use the "keyPress" method of the class "Robot".

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The "keyPress" method repeats the keystroke until the "keyRelease" method is called, possibly to repeat the keystroke it would be necessary to use a "Timer" or something more efficient.
      ACTUAL -
      The "keyPress" method displays a single keystroke and releases instantly even though the "ketReleased" method has not been called.

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

      public class Main {
          public static void main(String[] args) {
              try {
                  Robot robot = new Robot();

                  // How it currently works
                  robot.keyPress(KeyEvent.VK_A);
                  robot.delay(10000); // Wait for 10 seconds
                  robot.keyRelease(KeyEvent.VK_A);

                  System.out.println("Current result: a");

                  // How it should ideally work
                  for (int i = 0; i < 10; i++) {
                      robot.keyPress(KeyEvent.VK_A);
                      robot.delay(1000); // Wait for 1 second
                      robot.keyRelease(KeyEvent.VK_A);
                  }

                  System.out.println("Ideal result: aaaaaaaaaa");
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      }

      ---------- END SOURCE ----------

      FREQUENCY : always


        1. capture.pptx
          14.52 MB
        2. Main.java
          0.8 kB

            azvegint Alexander Zvegintsev
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: