[ACCEPTED]-How can I make Robot press and hold a mouse button for a certain period of time?-awtrobot

Accepted answer
Score: 12

Just sleep a bit between the two actions 1 (specified in milliseconds):

  1. Thread.sleep(long millis);

    robot.mousePress(InputEvent.BUTTON1_MASK);
    try { Thread.sleep(1000); } catch(Exception e) {} // Click one second
    robot.mouseRelease(InputEvent.BUTTON1_MASK);
    
  2. Robot.delay(long millis);

    robot.mousePress(InputEvent.BUTTON1_MASK);
    robot.delay(1000); // Click one second
    robot.mouseRelease(InputEvent.BUTTON1_MASK);
    
Score: 0

I did that, it's simple: when you detect 10 the mouse is pressed, you save the System.currentTimeMillis(). When 9 you detect the mouse is released, you just 8 check how long it was pressed.

If you want 7 the action to be made after a certain amount 6 of time, even if the mouse is still pressed, you 5 start a thread that lives the desired amount 4 of time when pressed and you interrupt it 3 when releasing. If the thread isn't interrupted 2 in the amount of time you wanted, the action 1 will be performed.

More Related questions