Forum Stats

  • 3,816,048 Users
  • 2,259,134 Discussions
  • 7,893,369 Comments

Discussions

Invalid key code

4066777
4066777 Member Posts: 2
edited Oct 5, 2019 8:11PM in Swing

***Moderator action (Timo): User this is an English language forum. Ask your question in English language, please!***

Translaton via Goggle:

A very simple program, Windows system, Java8 starts the following program in the German environment, always gives an error:

Exception in thread "main" java.lang.IllegalArgumentException: Invalid key code

at sun.awt.windows.WRobotPeer.keyPress(Native Method)

at java.awt.Robot.keyPress(Robot.java:354)

at testRobot.Main7.main(Main7.java:21)

code show as below;

import java.awt.AWTException;

import java.awt.Robot;

import java.awt.event.KeyEvent;

public class Main7 {

public static void main(String[] args) throws AWTException {

Robot robot = new Robot();

robot.setAutoDelay(300);

robot.delay(5000);

robot.keyPress(KeyEvent.VK_MINUS);

robot.keyRelease(KeyEvent.VK_MINUS);

robot.keyPress(KeyEvent.VK_OPEN_BRACKET );

robot.keyRelease(KeyEvent.VK_OPEN_BRACKET );

robot.keyPress(KeyEvent.VK_SEMICOLON);

robot.keyRelease(KeyEvent.VK_SEMICOLON );

robot.keyPress(KeyEvent.VK_QUOTE);

robot.keyRelease(KeyEvent.VK_QUOTE );

}

}

Is there any way to solve this, note (the above four key values represent German ßüöä)

------------------------------- ORIGINAL MESSAGE----------------------------

很简单的一个程序,Windows系统,Java8在德语环境下启动如下程序,总是报错:

Exception in thread "main" java.lang.IllegalArgumentException: Invalid key code

at sun.awt.windows.WRobotPeer.keyPress(Native Method)

at java.awt.Robot.keyPress(Robot.java:354)

at testRobot.Main7.main(Main7.java:21)

代码如下;

import java.awt.AWTException;

import java.awt.Robot;

import java.awt.event.KeyEvent;

public class Main7 {

public static void main(String[] args) throws AWTException {

Robot robot = new Robot();

robot.setAutoDelay(300);

robot.delay(5000);

robot.keyPress(KeyEvent.VK_MINUS);

robot.keyRelease(KeyEvent.VK_MINUS);

robot.keyPress(KeyEvent.VK_OPEN_BRACKET );

robot.keyRelease(KeyEvent.VK_OPEN_BRACKET );

robot.keyPress(KeyEvent.VK_SEMICOLON);

robot.keyRelease(KeyEvent.VK_SEMICOLON );

robot.keyPress(KeyEvent.VK_QUOTE);

robot.keyRelease(KeyEvent.VK_QUOTE );

}

}

有没有什么办法能够解决,备注(上面四个键值代表德语的ßüöä)

Answers

  • Jörg
    Jörg Member Posts: 1,301
    edited Oct 5, 2019 8:11PM

    The KeyEvent class does not define all unicode characters, but virtual keybord EVENTS only. So you cannot just replace äöüß of your German keyboard with the corresponding keys of a US keyboard. Keyboard events are mapped according to the keyboard language as far as possible, but German umlauts return always an event code of <span class="memberNameLink"><a>VK_UNDEFINED</a></span>. Thus the Robot class handles only a limited set of characters.

    You can however enter "[" (VK_OPEN_BRACKET) on a German keyboard via

    robot.keyPress(KeyEvent.VK_ALT_GRAPH);

    robot.keyPress(KeyEvent.VK_8);

    robot.keyRelease(KeyEvent.VK_8);

    robot.keyRelease(KeyEvent.VK_ALT_GRAPH);