Robot Class with Variable

Asked

Viewed 331 times

-2

I have a code and 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 care about the value that comes next to the word, I want the value of the variable to be written, can be varied throughout the code.

  • Isn’t it easier to monitor the entire word at once rather than letter by letter? If I write "cdamehera" its algorithm will count as camera too.

  • The rest of the code doesn’t really matter, what I really need is to learn how to take the variable and type its value elsewhere.

  • Yes, but since it is not very clear what this code should do, and as there is no way to execute, it is difficult to suggest something.

  • It has to do exactly what you are seeing, write "camera1", then it will add 1 in the variable and will have to write "camera2", as many times as necessary.

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 value "camera" with a number (or another string), there is that 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
  • I don’t understand how to do this

  • This will write the contents of the variable string letter by letter. That’s not your question?

  • Maybe it helps, but it wasn’t exactly that, I’m caring about the value that comes next to the word, I want the value of the variable to be written, can be varied throughout the code.

  • This should be described in your question, if there are more implementation details, edit the question and explain them. In the most: how to concatenate strings.

  • Actually that’s what I meant by "write the value of the variable" and "increasing the variable each time the word is written", but I’m editing anyway.

  • 1

    +1 Very good you show this method! Just a doubt, it is not necessary to call keyRelease() after the keyPress()? As for the question of @Lucasmoraesdesouza I think he wants the robot to write "camera1", then "camera2", then "camera3" ... until "Cameran" (where N is a number defined by him, and "camera" is a String prefix also defined by him). That’s right Luke?

  • @Douglas I don’t know, I don’t usually use this class. But it makes sense his questioning, until I found a question on stackoverflow on the subject. It may have side effect in case of combinations of type "Ctrl + c", where "C" should be pressed with Ctrl also pressed. But I edited the question, better to be sure. The rest of the doubt is basically concatenating strings, there are already "n" questions on the subject here.

  • @Renan, I don’t understand how string concatenation can help me with this, if you know, I ask you, please, an explanation.

  • @Douglas yes, that’s exactly it.

  • @Lucasmoraesdesouza response edited.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.