Robot Class with Variable

-2

I have a code and I need to write the value of a variable through the robot class, for example:

int camera=2
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_C);
robot.keyPress(KeyEvent.VK_A);
robot.keyPress(KeyEvent.VK_M);
robot.keyPress(KeyEvent.VK_E);
robot.keyPress(KeyEvent.VK_R);
robot.keyPress(KeyEvent.VK_A);

I want to be able to change the value of the variable,     

asked by anonymous 28.06.2017 / 13:42

1 answer

3

Use KeyEvent.getExtendedKeyCodeForChar() :

final String string = "camera";

for(char character : string.toCharArray()){
   robot.keyPress(KeyEvent.getExtendedKeyCodeForChar(character));
   robot.keyRelease(KeyEvent.getExtendedKeyCodeForChar(character));
}

If you need to concatenate the "camera" value with a number (or another string), there is this question that already addresses the subject.

final int MAX = 5;
final Robot robot = new Robot();

for(int i = 1; i <= MAX; i++){
   final String string = "camera" + i;
   for(char character : string.toCharArray()){
      robot.keyPress(KeyEvent.getExtendedKeyCodeForChar(character));
      robot.keyRelease(KeyEvent.getExtendedKeyCodeForChar(character));
   }
   // só para pular linha no output, não são necessárias as duas linhas abaixo.
   robot.keyPress(KeyEvent.VK_ENTER);
   robot.keyRelease(KeyEvent.VK_ENTER);
}

output:

camera1
camera2
camera3
camera4
camera5
    
28.06.2017 / 14:27