java模拟鼠标,自动化操作小案例

/**
 * 获取屏幕位置,前提是屏幕分辨率设置成100%
 */
public class MousePointer {
    public static void main(String[] args) {
        getPointInfo();
    }

    private static void getPointInfo() {
        int x = 0;
        int y = 0;
        while (true) {
            PointerInfo pinfo = MouseInfo.getPointerInfo();
            int mx = pinfo.getLocation().x;
            int my = pinfo.getLocation().y;
            if (x != mx || y != my) {
                x = mx;
                y = my;
                System.out.println("x:" + mx + ",y:" + my);
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                System.out.println(e.getMessage());
            }
        }
    }
}
    private static Robot robot;

    static {
        try {
            robot = new Robot();
        } catch (AWTException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) throws Exception {
        int i = 0;
        while (true){
            Thread.sleep(300);
            i++;
            if(i == 30){
                System.out.println(i);
                break;
            }
            //登录
            mousePressAndReleaseForpoint(1444,366);
            robot.delay(50);
            //返回
            mousePressAndReleaseForpoint(1788,916);
        }
    }

    /**
     * 移动到指定位置,点击鼠标左键
     * @param x
     * @param y
     */
    private static void mousePressAndReleaseForpoint(int x,int y){
        robot.mouseMove(x,y);
        robot.delay(1);
        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        robot.delay(1);
        robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
        robot.delay(1);
    }

你可能感兴趣的:(java,自动化,开发语言)