Java Robot mouse move: setting speed?

2019-04-10 22:43发布

问题:

The Java Robot class allows one to move the mouse as if the actual physical mouse was moved.

However, how does one move the mouse from Point1 to Point2 in a humane (and thus not instant) manner? Aka, how does one set the speed of movement?

If no such speed is possible with the Robot class, thus if the mouse can only be moved instantenously, what kind of "algorithm" should be used to mimic a human's mouse movement? Should it move the mouse pixel by pixel with a certain incrementing speed?

回答1:

The Robot class has a delay(...) method that you can use to control movement from point to point. Try a few different alogorithms to determine what you like.



回答2:

Here is a pretty good way here:

Consider start_x where your mouse starts and end_x where you are wanting it to end. Same for y

for (int i=0; i<100; i++){  
    int mov_x = ((end_x * i)/100) + (start_x*(100-i)/100);
    int mov_y = ((end_y * i)/100) + (start_y*(100-i)/100);
    robot.mouseMove(mov_x,mov_y);
    robot.delay(10);
}

Hope that helps...