首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >模拟人类鼠标移动的Java Robot类

模拟人类鼠标移动的Java Robot类
EN

Stack Overflow用户
提问于 2012-08-26 23:37:39
回答 2查看 11.9K关注 0票数 10

我正在做一个关于远程控制的项目,从客户端发送光标的内容x和y到服务器。

代码语言:javascript
复制
robot.mouseMove(x,y);

将仅将光标移动到特定点,而不会将光标从原始点移动

我找到了这个简单的算法来模拟鼠标的连续运动。

代码语言:javascript
复制
for (int i=0; i<100; i++){
   int x = ((end_x * i)/100) + (start_x*(100-i)/100);
 int y = ((end_y * i)/100) + (start_y*(100-i)/100);
 robot.mouseMove(x,y);
} 

但是这个算法仍然太简单了,它只是从一个点缓慢地移动到另一个点,这仍然不像人类的行为。

我读过一些关于网络远程控制的开源代码,我发现这个项目http://code.google.com/p/java-remote-control/使用了来自MouseListener类的方法调用MosueMovement,他们用它来执行“拖动”。

我想知道有没有人知道更好的方法?

EN

回答 2

Stack Overflow用户

发布于 2018-10-21 06:20:02

对于将来的任何人:我为Java开发了一个库,它模仿人类的鼠标移动。移动中的噪声/锯齿,正弦弧线,位置过冲等。另外,该库在编写时考虑了扩展和配置的可能性,因此,如果默认解决方案与情况不匹配,任何人都可以对其进行微调。现在可从Maven Central获得。

https://github.com/JoonasVali/NaturalMouseMotion

票数 6
EN

Stack Overflow用户

发布于 2012-08-26 23:59:01

看看我写的这个例子。您可以对此进行改进以模拟Joey所说的话。我写得非常快,有很多地方可以改进(算法和类设计)。请注意,我只处理从左到右的移动。

代码语言:javascript
复制
import java.awt.AWTException;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Robot;

public class MouseMoving {

    public static void main(String[] args) {
        new MouseMoving().execute();
    }

    public void execute() {
        new Thread( new MouseMoveThread( 100, 50, 50, 10 ) ).start();
    }

    private class MouseMoveThread implements Runnable {

        private Robot robot;
        private int startX;
        private int startY;
        private int currentX;
        private int currentY;
        private int xAmount;
        private int yAmount;
        private int xAmountPerIteration;
        private int yAmountPerIteration;
        private int numberOfIterations;
        private long timeToSleep;

        public MouseMoveThread( int xAmount, int yAmount,
                int numberOfIterations, long timeToSleep ) {

            this.xAmount = xAmount;
            this.yAmount = yAmount;
            this.numberOfIterations = numberOfIterations;
            this.timeToSleep = timeToSleep;

            try {

                robot = new Robot();

                Point startLocation = MouseInfo.getPointerInfo().getLocation();
                startX = startLocation.x;
                startY = startLocation.y;

            } catch ( AWTException exc ) {
                exc.printStackTrace();
            }

        }

        @Override
        public void run() {

            currentX = startX;
            currentY = startY;

            xAmountPerIteration = xAmount / numberOfIterations;
            yAmountPerIteration = yAmount / numberOfIterations;

            while ( currentX < startX + xAmount &&
                    currentY < startY + yAmount ) {

                currentX += xAmountPerIteration;
                currentY += yAmountPerIteration;

                robot.mouseMove( currentX, currentY );

                try {
                    Thread.sleep( timeToSleep );
                } catch ( InterruptedException exc ) {
                    exc.printStackTrace();
                }

            }

        }

    }

}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12131427

复制
相关文章

相似问题

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