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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Jeel Shah
  • 3,274
  • 17
  • 47
  • 68
  • 3
    What isn't working about it, exactly? Could you give some example input/output? – Maxpm May 02 '11 at 21:11
  • yea sure, if you input for example "http://youtube.com" it will throw exception because ":" is not recognized or something – Jeel Shah May 02 '11 at 21:16
  • possible duplicate of [simulate backspace key with java.awt.Robot](http://stackoverflow.com/questions/2596641/simulate-backspace-key-with-java-awt-robot) – trashgod May 02 '11 at 21:21
  • So add some special cases in for ones you want to support, and catch and log exceptions to that. – corsiKa May 02 '11 at 21:21
  • yes, i have taken that into account but it seems inefficent, is there no easier way? – Jeel Shah May 02 '11 at 21:22
  • 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`...? – Rom1 May 02 '11 at 21:24
  • There is a textbox, where they enter text and i input it on the screen. – Jeel Shah May 02 '11 at 21:25
  • Well then, get the `KeyEvent`s sent to the text box, and then save them and replay them. 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) – Rom1 May 02 '11 at 21:29

2 Answers2

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)

Rom1
  • 3,167
  • 2
  • 22
  • 39
1

Robot expects key codes defined in KeyEvent.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045