Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make Robot press and hold a mouse button for a certain period of time?

I am using Java to generate a mouse press using the Robot class:

robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);

However, I want the Robot to press the button for a certain period of time. How can I achieve this?

like image 775
Neutralise Avatar asked Jun 19 '11 08:06

Neutralise


1 Answers

Just sleep a bit between the two actions (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);
    
like image 193
Martijn Courteaux Avatar answered Sep 29 '22 18:09

Martijn Courteaux