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

Robot cant press/release KeyCode which is given by the KeyEvent, when typing an umlaut

XMLWordPrintable

    • x86
    • generic

      FULL PRODUCT VERSION :
      java version "1.8.0_77"
      Java(TM) SE Runtime Environment (build 1.8.0_77-b03)
      Java HotSpot(TM) 64-Bit Server VM (build 25.77-b03, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 10.0.10586]

      A DESCRIPTION OF THE PROBLEM :
      I want to let the java.awt.Robot press and release a earlier typed key (like playing a keyboard record). But this is not possible if the Keyboard Layout is NOT the International (like in my case its a german layout), with umlauts (ü,ö,ä). Because the given KeyCode through the KeyEvent while pressing an umlaut, throws an IllegalArgumentException.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Switch the Keyboard Layout to a non-international Layout. Record the given KeyCode by typing an umlaut. And let the Robot press/release this recorded KeyCode.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I expected it will press the key on my System, which i typed before.
      ACTUAL -
      I got an Exception. (See: Error Message)

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      java.lang.IllegalArgumentException: Invalid key code
      at sun.awt.windows.WRobotPeer.keyPress(Native Method)
      at java.awt.Robot.keyPress(Robot.java:354)
      at me.catching.KeyListener.nativeKeyPressed(KeyListener.java:130)
      at org.jnativehook.GlobalScreen$EventDispatchTask.processKeyEvent(Unknown Source)
      at org.jnativehook.GlobalScreen$EventDispatchTask.run(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at java.lang.Thread.run(Thread.java:745)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.AWTException;
      import java.awt.Robot;
      import java.awt.event.*;
      import javax.swing.*;

      public class KeyEventDemo implements KeyListener {
          
      static JFrame frame = new JFrame("KeyEventDemo");

      public static void main(String[] args) {
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(500, 500);
      JTextField typingArea = new JTextField(20);
              typingArea.addKeyListener(new KeyEventDemo());
              frame.add(typingArea);
              frame.pack();
      frame.setVisible(true);

      }

          /** Handle the key typed event from the text field. */
          public void keyTyped(KeyEvent e) {
          }
          
          /** Handle the key pressed event from the text field. */
          public void keyPressed(KeyEvent e) {
              System.out.println("KeyCode: " + e.getKeyCode());
              System.out.println("ExtendedKeyCode: " + e.getExtendedKeyCode());
              
              try {
      Robot robot = new Robot();

      robot.delay(2000); //Short Wait. Because its better to click out of this Window to have no while
      robot.keyPress(e.getExtendedKeyCode());
      robot.keyRelease(e.getExtendedKeyCode());
      robot.keyPress(e.getKeyCode());//Nothing of these versions work
      robot.keyRelease(e.getKeyCode());

      } catch (AWTException e1) {
      e1.printStackTrace();
      }
              
          }
          
          /** Handle the key released event from the text field. */
          public void keyReleased(KeyEvent e) {
          }
          
      }
      ---------- END SOURCE ----------

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated: