Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
115 views
in Technique[技术] by (71.8m points)

java - How can I make Robot type a `:`?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...