1

I have a problem with typing in Robot Class. I want the robot to type something the user has entered. The robot for some reason can't type some of the characters. Here is my type code:

public void type(String s,Robot robot) {
    byte[] stringBytes = s.getBytes();

    for (byte b : stringBytes) {
        int code = b;

        if (code > 96 && code < 123)
            code = code - 32;
        robot.keyPress(code);
        robot.keyRelease(code);
    }
}

how can i fix this problem?

8
  • 3
    What isn't working about it, exactly? Could you give some example input/output? Commented May 2, 2011 at 21:11
  • yea sure, if you input for example "youtube.com" it will throw exception because ":" is not recognized or something Commented May 2, 2011 at 21:16
  • possible duplicate of simulate backspace key with java.awt.Robot Commented May 2, 2011 at 21:21
  • So add some special cases in for ones you want to support, and catch and log exceptions to that. Commented May 2, 2011 at 21:21
  • yes, i have taken that into account but it seems inefficent, is there no easier way? Commented May 2, 2011 at 21:22

2 Answers 2

2

If you want to "type back what the user entered", then surely you should be capturing a set of KeyEvent objects, and not a String. There is not a key for every String character, far from it! (for instance you need to press 'shift' to input a colon, so that's two key presses and not one)

Sign up to request clarification or add additional context in comments.

Comments

1

Robot expects key codes defined in KeyEvent.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.