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