Java Robot mouse move: setting speed?

2019-04-10 22:21发布

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?

2条回答
放荡不羁爱自由
2楼-- · 2019-04-10 22:48

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.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-04-10 22:58

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...

查看更多
登录 后发表回答