使鼠标移动像人一样(使用弧形而不是到目的地的直线) [英] Making mouse movements humanlike (using an arc rather than a straight line to the destination)

查看:80
本文介绍了使鼠标移动像人一样(使用弧形而不是到目的地的直线)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用java.awt.Robot制作自动点击器.但是,我担心的问题之一是动作并不十分人性化.有人可以建议对我的代码进行一些更改,使其更像人吗?现在,它只是直线移动.

I am making and auto clicker using java.awt.Robot. One of the concerns i have however is the movements aren't very humanlike. Can anyone suggest some changes to my code to make it more human like? Right now it just moves in a straight line.

/**
 * 
 * @param robot The java.awt.Robot being utilized
 * @param sx The start x position of the mouse
 * @param sy The start y potition of the mouse
 * @param ex The end x position of the mouse
 * @param ey The end y position of the mouse
 * @param speed The speed at which to travel
 */
public void moveMouse(Robot robot, int sx, int sy, int ex, int ey, int speed){
    for (int i=0; i<100; i++){  
        int mov_x = ((ex * i)/100) + (sx*(100-i)/100);
        int mov_y = ((ey * i)/100) + (sy*(100-i)/100);
        robot.mouseMove(mov_x,mov_y);
        robot.delay(speed);
    }

}

更新:我决定采用一种使用贝塞尔曲线的算法.自从实施更改以来已经很长时间了,但是我想将其发布在这里,以防人们将来发现它有用.这就是我最终得到的:

Update: I decided to go with an algorithm that makes use of Bézier Curves. It's been a very long time since I implemented the change, but I wanted to post it here just in case people would find it useful in the future. Here is what I ended up with:

public class MouseEvent{
    public int getMouseX(){
        return MouseInfo.getPointerInfo().getLocation().x;
    }

    public int getMouseY(){
        return MouseInfo.getPointerInfo().getLocation().y;
    }

    public void moveMouse(int speed, int destX, int destY, int ranX, int ranY){
        Mouse.moveMouse(new Robot(), new Point(getMouseX(),getMouseY()), new Point(destX, destY), speed, ranX, ranY);
    }
}

public class Mouse {
    public static void moveMouse(Robot robot, Point s, Point e, int speed, int ranX, int ranY){
        if(Math.abs(e.x-s.x) <= ranX && Math.abs(e.y-s.y) <= ranY)
            return;

        Point[] cooardList;
        double t;    //the time interval
        double k = .025;
        cooardList = new Point[4];

        //set the beginning and end points
        cooardList[0] = s;
        cooardList[3] = new Point(e.x+random(-ranX,ranX),e.y+(random(-ranY,ranY)));

        int xout = (int)(Math.abs(e.x - s.x) /10);
        int yout = (int)(Math.abs(e.y - s.y) /10);

        int x=0,y=0;

        x = s.x < e.x 
            ? s.x + ((xout > 0) ? random(1,xout) : 1)
            : s.x - ((xout > 0) ? random(1,xout) : 1);
        y = s.y < e.y 
            ? s.y + ((yout > 0) ? random(1,yout) : 1)
            : s.y - ((yout > 0) ? random(1,yout) : 1);
        cooardList[1] = new Point(x,y);

        x = e.x < s.x 
            ? e.x + ((xout > 0) ? random(1,xout) : 1)
            : e.x - ((xout > 0) ? random(1,xout) : 1);
        y = e.y < s.y 
            ?  e.y + ((yout > 0) ? random(1,yout) : 1)
            : e.y - ((yout > 0) ? random(1,yout) : 1);
        cooardList[2] = new Point(x,y);

        double px = 0,py = 0;
        for(t=k;t<=1+k;t+=k){
            //use Berstein polynomials
            px=(cooardList[0].x+t*(-cooardList[0].x*3+t*(3*cooardList[0].x-
                cooardList[0].x*t)))+t*(3*cooardList[1].x+t*(-6*cooardList[1].x+
                cooardList[1].x*3*t))+t*t*(cooardList[2].x*3-cooardList[2].x*3*t)+
                cooardList[3].x*t*t*t;
            py=(cooardList[0].y+t*(-cooardList[0].y*3+t*(3*cooardList[0].y-
                cooardList[0].y*t)))+t*(3*cooardList[1].y+t*(-6*cooardList[1].y+
                cooardList[1].y*3*t))+t*t*(cooardList[2].y*3-cooardList[2].y*3*t)+
                cooardList[3].y*t*t*t;
            robot.mouseMove((int)px, (int)py);
            robot.delay(random(speed,speed*2));
        }
    }    
}

推荐答案

 public void moveMouse(int sx, int sy, int ex, int ey, int speed) throws AWTException {
        Robot robot = new Robot();
        int a = 10;
        boolean flag = true;
        for (int i = 0; i < 100; i++) {
            int mov_x = ((ex * i) / 100) + (sx * (100 - i) / 100);
            int mov_y = ((ey * i) / 100) + (sy * (100 - i) / 100);
            if (flag == true) {
                robot.mouseMove(mov_x + a, mov_y); // adds 10 to X-axis
                flag = false;
            } else {
                robot.mouseMove(mov_x - 2 * a, mov_y); // subtracts 20 to X-axis
                flag = true;
            }
            robot.delay(speed);
        }
    }

只需操纵您的代码即可.这将鼠标沿X方向直线移动.您可以从这里实现您想要的.只是想出主意.如果可以操纵 mov_x mov_y ,则可以随意移动.

Just manipulated your code. This moves the mouse in straight path in X-direction. You can achieve what you want from here. Just get the ideas. You can move any way you want if you can manipulate mov_x and mov_y .

这篇关于使鼠标移动像人一样(使用弧形而不是到目的地的直线)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆