在Java中以编程方式模拟鼠标拖动

时间:2017-04-25 17:50:21

标签: java mouseevent

有没有办法在Java中模拟鼠标拖动事件。我可以使用java.awt.Robot模拟鼠标点击和鼠标移动。但是,我无法模拟鼠标拖动的动作。

我尝试让机器人类使用,robot.mousePress()按住鼠标按钮,并在调用robot.mouseRelase之前移动鼠标位置,同时在鼠标移动期间暂停几次。但是,所有这一切都是模拟鼠标光标移动并且不模拟鼠标拖动事件。

我将包含我正在使用的代码片段;然而,它所做的只是我上面提到的,单击鼠标并移动鼠标光标。

我在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);

}

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题。以下是我回答的其他问题的链接: https://stackoverflow.com/a/45063135/

添加thread.sleep();是进行拖动动作的正确解决方案。但是你把它添加到了错误的地方。您需要确保在按下鼠标时光标正在移动。为了挂起线程mousePressed,你必须在mousePressed和mouseMoved之间添加thread.sleep。

请参阅以下代码:

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