0
有没有一种方法来模拟Java中的鼠标拖动事件。我可以使用java.awt.Robot来模拟鼠标点击和鼠标移动。但是,我无法模拟鼠标拖动的操作。模拟鼠标以编程方式在Java中拖动
我尝试让机器人类使用robot.mousePress()按住鼠标按钮并在调用robot.mouseRelease之前在鼠标移动过程中暂停多次时移动鼠标位置。但是,所有这些都是模拟鼠标光标移动并且不模拟鼠标拖动事件。
我将包含我正在使用的代码片段;然而,它所做的只是我上面提到的,单击鼠标并移动鼠标光标。
我在Windows 7上运行这个应用程序。
在此先感谢。
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);
}