9

I want to type : using Java Robot. However, I'm getting an IllegalArgumentException. My code is:

robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_COLON);
robot.keyRelease(KeyEvent.VK_COLON);
robot.keyRelease(KeyEvent.VK_SHIFT);

The exception is:

java.lang.IllegalArgumentException: Invalid key code.].

I also tried with:

robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_SEMICOLON);
robot.keyRelease(KeyEvent.VK_SEMICOLON);
robot.keyRelease(KeyEvent.VK_SHIFT);

How can I solve this problem?

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
Eric Bautista
  • 403
  • 3
  • 8
  • 18

8 Answers8

17

try with this code :

robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_SEMICOLON);
robot.keyRelease(KeyEvent.VK_SEMICOLON);
robot.keyRelease(KeyEvent.VK_SHIFT);

As with the keyboard you enter : when you press shift + ;. the same you need to simulate.

Try running this code just to try out which works fine with above answer:

public class Test {
    public static void main(String[] args) {
        Robot robot;
        try {
            robot = new Robot();
            robot.keyPress(KeyEvent.VK_SHIFT);  
            robot.keyPress(KeyEvent.VK_SEMICOLON);  
            robot.keyRelease(KeyEvent.VK_SEMICOLON);  
            robot.keyRelease(KeyEvent.VK_SHIFT);
        } catch (AWTException e) {
            // TODO Auto-generated catch bloc
            e.printStackTrace();
        }


    }
}
GuruKulki
  • 25,776
  • 50
  • 140
  • 201
  • The result is the same: Exception in thread "main" java.lang.IllegalArgumentException: Invalid key code at sun.awt.windows.WRobotPeer.keyPress(Native Method) at java.awt.Robot.keyPress(Unknown Source) – Eric Bautista Apr 20 '11 at 20:38
  • 1
    The problem is the semicolon keyEvent. If I execute this code, I get the same Exception but the shift key is "still pressed" after terminating it. Does this code work for you? – Eric Bautista Apr 20 '11 at 21:09
  • @Eric on my Windows XP box the above code works fine. If you are still getting an error I would try to just do a `keyPress(KeyEvent.VK_COLON)` without the `VK_SHIFT` modifier. – Tim Bender Apr 20 '11 at 21:12
4

Unfortunately, Java Robot class relies on a platform specific implementation of an undocumented interface called java.awt.peer.RobotPeer. The platform specific implementation decides what key press events are legal or illegal.

On my windows XP box, this works fine:

public static void main(final String[] args) throws InterruptedException, IOException {
    Robot robot;
    try {
        robot = new Robot();
        robot.keyPress(KeyEvent.VK_SHIFT);
        robot.keyPress(KeyEvent.VK_SEMICOLON);
        robot.keyRelease(KeyEvent.VK_SEMICOLON);
        robot.keyRelease(KeyEvent.VK_SHIFT);
    } catch (final AWTException e) {
        // TODO Auto-generated catch bloc
        e.printStackTrace();
    }
}

On a different platform you may want to try:

public static void main(final String[] args) throws InterruptedException, IOException {
    Robot robot;
    try {
        robot = new Robot();
        robot.keyPress(KeyEvent.VK_COLON);
        robot.keyRelease(KeyEvent.VK_COLON);
    } catch (final AWTException e) {
        // TODO Auto-generated catch bloc
        e.printStackTrace();
    }
}
Tim Bender
  • 20,112
  • 2
  • 49
  • 58
2

try this code ;), maybe it helps (using ascii code alt+5+8=:):

robot9.delay(20);
robot9.keyPress(KeyEvent.VK_ALT);
robot9.delay(20);
robot9.keyPress(KeyEvent.VK_NUMPAD5);
robot9.keyRelease(KeyEvent.VK_NUMPAD5);
robot9.delay(20);
robot9.keyPress(KeyEvent.VK_NUMPAD8);
robot9.keyRelease(KeyEvent.VK_NUMPAD8);
robot9.delay(20);
robot9.keyRelease(KeyEvent.VK_ALT);
robot9.delay(20);
Misose
  • 21
  • 2
  • Thank you. The concept works with : and also \ signs. So I can type file location to a pop-up file browser. You saved me some hours. Thx. – David Feb 27 '18 at 12:35
2

This also seems to be language dependent. On a German keyboard, using the combination of VK_SHIFT and VK_PERIOD worked.

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
0

Try This Code


        case KeyEvent.VK_SEMICOLON:
            if((event.getModifiers() & KeyEvent.KEY_PRESSED)!=0)
               System.out.println(":");
            else
                System.out.print(";");
            break;
Nirav Dabhi
  • 341
  • 1
  • 7
  • 29
0

Someone build a KeyboardKeys class and published it here in SO. it´s at https://stackoverflow.com/a/20979488/7069565. In a nutshell, he types every character as an Alt + Number combination.

Community
  • 1
  • 1
Omar N
  • 61
  • 7
0

Semicolon is an "upercase leter", that is, you only get it with the combination of Keys

Shift+Coma

Try this:

robot = new Robot();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_COMMA);
//Since you have the Shift pressed it will generate a semi colon.
robot.keyRelease(KeyEvent.VK_COMMA);
robot.keyRelease(KeyEvent.VK_SHIFT);

I hope I have helped.

Have a nice day. :)

Saclyr Barlonium
  • 453
  • 4
  • 12
0

I don't know about Java Robots, but if you're using shift, shouldn't you then type semicolon, because shift + semicolon = colon. So it's probably an illegal argument because colon isn't a key, semicolon is.

None
  • 3,875
  • 7
  • 43
  • 67