2017-04-25 112 views
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); 

}

回答

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