首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Java编程模拟鼠标拖动

用Java编程模拟鼠标拖动
EN

Stack Overflow用户
提问于 2017-04-26 01:50:21
回答 1查看 1.4K关注 0票数 0

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

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

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

我在Windows 7上运行此应用程序。

提前谢谢。

代码语言:javascript
复制
   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);

}

EN

回答 1

Stack Overflow用户

发布于 2017-07-13 00:42:48

我遇到了和以前类似的问题。下面是我回答的另一个问题的链接:https://stackoverflow.com/a/45063135/

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

代码如下:

代码语言:javascript
复制
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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43617692

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档