How can I make Robot type a `:`?

时光怂恿深爱的人放手 提交于 2019-11-28 01:55:20

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


    }
}

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

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

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

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.

Try This Code


        case KeyEvent.VK_SEMICOLON:
            if((event.getModifiers() & KeyEvent.KEY_PRESSED)!=0)
               System.out.println(":");
            else
                System.out.print(";");
            break;
Omar N

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.

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. :)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!