0

i created this robot:

   try {
       // Create class
       Robot robot = new Robot();

       // Wait 2 sec
       robot.delay(2000);

       // Get mouse on "texteditor"-symbol
       robot.mouseMove(920, 840);

       // Press it
       robot.mousePress( InputEvent.BUTTON1_MASK );
       robot.delay( 100 );
       robot.mouseRelease( InputEvent.BUTTON1_MASK );

       // Wait 2 sec
       robot.delay( 2000 );

       // Write "Hello" in "texteditor"
       robot.keyPress(KeyEvent.VK_H);
       robot.delay( 1000 );
       robot.keyPress(KeyEvent.VK_E);
       robot.delay( 1000 );
       robot.keyPress(KeyEvent.VK_L);
       robot.delay( 1000 );
       robot.keyPress(KeyEvent.VK_L);
       robot.delay( 1000 );
       robot.keyPress(KeyEvent.VK_O);

   } catch (AWTException e) {
       e.printStackTrace();
   }

ok what happens, when i launch programm? :

  1. Robot waits 2 sec. => good

  2. Mouse is moved on texteditor-symbol => good

  3. It is pressed => texteditor opens => good

  4. Robot wait´s 2 sec => good

  5. Robot writes "l" in the texteditor, the rest of the word can be found in the source code of the robot => not good !

Any advices what i did wrong or how i could do this? greetings and thanks!!

eltabo
  • 3,749
  • 1
  • 21
  • 33
user3297073
  • 129
  • 1
  • 12

1 Answers1

1

As per the documentation:

public void keyPress(int keycode)

Presses a given key. The key should be released using the keyRelease method. (...)

Your code only presses keys, and never releases them. Open your favourite text editor, and try manually to write "hello" without releasing any key you press. What happens when you have to type the second L? The key is already pressed.

Try the following code, with the appropriate releases:

robot.keyPress(KeyEvent.VK_H);
robot.keyRelease(KeyEvent.VK_H);
robot.delay( 1000 );
robot.keyPress(KeyEvent.VK_E);
robot.keyRelease(KeyEvent.VK_E);
robot.delay( 1000 );
    /* ... */
Community
  • 1
  • 1
afsantos
  • 5,178
  • 4
  • 30
  • 54