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);
}