Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make Robot type a `:`?

Tags:

java

awtrobot

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?

like image 201
Eric Bautista Avatar asked Apr 20 '11 20:04

Eric Bautista


5 Answers

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);
like image 117
Misose Avatar answered Nov 02 '22 10:11

Misose


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.

like image 30
Omar N Avatar answered Nov 02 '22 09:11

Omar N


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

like image 22
Thomas Weller Avatar answered Nov 02 '22 09:11

Thomas Weller


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();
        }


    }
}
like image 24
GuruKulki Avatar answered Nov 02 '22 09:11

GuruKulki


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();
    }
}
like image 4
Tim Bender Avatar answered Nov 02 '22 09:11

Tim Bender