0

Is there a way to simulate mouse drag events in Java. I can simulate mouse clicks and mouse movements using the java.awt.Robot. However, I cannot simulate the action of a mouse drag.

I tried having the robot class keep the mouse button depressed using, robot.mousePress(), and moving the mouse location while pausing several times during the mouse move before calling robot.mouseRelase. However, all this does is simulate the mouse cursor moving and does not simulate a mouse drag event.

I will include a snippet of the code I am using; however, all it does is just what I mentioned above, click the mouse and move the mouse cursor.

I am running this applications on Windows 7.

Thanks in advance.

   public void click() throws AWTException, InterruptedException {
  int numberOfMoveIterations = 15;

  Robot bot = new Robot();
  Thread.sleep(mouseDownDelayClickTime + this.delayTime);
  bot.mouseMove((int) (mouseClickDownXLocation * this.widthEventOffset), (int) (mouseClickDownYLocation * this.heightEventOffset));
  bot.mousePress(InputEvent.BUTTON1_MASK);
  if (mouseClickDownXLocation != mouseClickUpXLocation || mouseClickDownYLocation != mouseClickUpYLocation) {
     int xAmountToMove = mouseClickUpXLocation - mouseClickDownXLocation;
     int yAmountToMove = mouseClickUpYLocation - mouseClickDownYLocation;
     int xAmountPerIteration = xAmountToMove / numberOfMoveIterations;
     int yAmountPerIteration = yAmountToMove / numberOfMoveIterations;

     int currentXLocation = mouseClickDownXLocation;
     int currentYLocation = mouseClickDownYLocation;

     while (currentXLocation < mouseClickUpXLocation + xAmountToMove
          && currentYLocation < mouseClickUpYLocation + yAmountToMove) {
        currentXLocation += xAmountPerIteration;
        currentYLocation += yAmountPerIteration;

        bot.mouseMove(currentXLocation, currentYLocation);
        Thread.sleep(mouseUpDelayClickTime);

     }
  }
  bot.mouseRelease(InputEvent.BUTTON1_MASK);

}

user1836443
  • 53
  • 1
  • 5

1 Answers1

0

I had a similar problem as before. Here is the link for the other question I have answered : https://stackoverflow.com/a/45063135/

Adding the thread.sleep(); is the right solution for making a drag motion. But you add it to the wrong place. You need to make sure the cursor is moving while the mouse is pressed. In order to suspend the thread mousePressed, you will have to add thread.sleep between mousePressed and mouseMoved.

See the codes as below:

public void click() throws AWTException, InterruptedException {
int numberOfMoveIterations = 15;

Robot bot = new Robot();
thread.sleep(mouseDownDelayClickTime + this.delayTime);
bot.mouseMove((int) (mouseClickDownXLocation * this.widthEventOffset), (int) (mouseClickDownYLocation * this.heightEventOffset));
bot.mousePress(InputEvent.BUTTON1_MASK);

/* suspend the current thread here */
thread.sleep(mouse pressed thread suspended time);  

if (mouseClickDownXLocation != mouseClickUpXLocation || mouseClickDownYLocation != mouseClickUpYLocation) {
  int xAmountToMove = mouseClickUpXLocation - mouseClickDownXLocation;
  int yAmountToMove = mouseClickUpYLocation - mouseClickDownYLocation;
  int xAmountPerIteration = xAmountToMove / numberOfMoveIterations;
  int yAmountPerIteration = yAmountToMove / numberOfMoveIterations;

  int currentXLocation = mouseClickDownXLocation;
  int currentYLocation = mouseClickDownYLocation;

  while (currentXLocation < mouseClickUpXLocation + xAmountToMove
      && currentYLocation < mouseClickUpYLocation + yAmountToMove) {
    currentXLocation += xAmountPerIteration;
    currentYLocation += yAmountPerIteration;

    bot.mouseMove(currentXLocation, currentYLocation);

 }
}
bot.mouseRelease(InputEvent.BUTTON1_MASK);