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