3

我正在使用 java.awt.Robot 制作和自动点击器。然而,我担心的一个问题是这些动作不是很人性化。任何人都可以建议对我的代码进行一些更改以使其更人性化吗?现在它只是直线移动。

/**
 * 
 * @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);
    }

}

更新: 我决定使用一种利用Bézier Curves的算法。自从我实施更改以来已经很长时间了,但我想在这里发布它,以防人们将来发现它有用。这是我最终得到的结果:

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));
        }
    }    
}
4

2 回答 2

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

于 2011-12-16T14:09:38.727 回答
1

您可以使用 Catmull-Rom 方法。在端点周围的某处生成随机控制点,可能是直线所在的位置,询问从开始到结束移动的每一步的坐标(参数 t,从零到一)。

请参阅演示小程序和来源:http ://www.cse.unsw.edu.au/~lambert/splines/

于 2011-12-16T13:57:18.547 回答