-1

I got a java robot to type characters, however it prints stuff like:

.. 5./. .. .. //5 / /55/ /.. ..5.. .. 5 5 . 5.

Instead of the wanted string. Does someone know how to avoid this?

import java.awt.Robot;
import java.awt.AWTException;

Robot robot;

String txt = "o noes ";
char[] chars;

void setup() {

  chars = txt.toCharArray();

  try {
    robot = new Robot();
  } 
  catch(AWTException e) {
  }
  robot.setAutoDelay(1);

  for (int i = 0; i < 10000; i++) {
    int c = chars[(int)random(chars.length)];
    robot.keyPress(c);
    robot.keyRelease(c);
  }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
clankill3r
  • 9,146
  • 20
  • 70
  • 126

2 Answers2

2

You have to use values from the KeyEvent class, not characters, for keyPress, as per the javadoc for keyPress.

KeyEvent ke = new KeyEvent(<various parameters>);
ke.setKeyChar('a');
int code = ke.getKeyCode();
bmargulies
  • 97,814
  • 39
  • 186
  • 310
2

You might consider this kind of lengthy but it works http://pastebin.com/p0BdJxpy

XliteRSPS
  • 106
  • 10