简体   繁体   English

Java:机械手移动未知错误

[英]Java: Robot Mouse Move Unknown Error

Summary: I am creating a program that moves the user's mouse to a predetermined, on-screen coordinate. 简介:我正在创建一个程序,将用户的鼠标移动到预定的屏幕坐标上。 The program shifts the mouse, coordinate by coordinate, to simulate human motion. 该程序逐个坐标移动鼠标以模拟人的动作。 The issue is that the program only moves the mouse to one of the coordinates (whichever it reaches first) and immediately after stops. 问题是该程序仅将鼠标移动到坐标之一(无论它先到达哪个坐标),并在停止后立即移动。 I need it to shift the mouse to the correct coordinate. 我需要将鼠标移至正确的坐标。

import java.awt.AWTException;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.Robot;
import java.util.Random;

public class MouseMover implements Runnable {

    Robot robot;
    Random random = new Random();

    int dX, // destination x-coord
            dY, // destination y-coord
            cX,
            cY;

    PointerInfo pointerInfo;
    Point point;

    public MouseMover(int mX, int mY) throws AWTException {
        robot = new Robot();

        dX = mX;
        dY = mY;
    }

    @Override
    public void run() {
        pointerInfo = MouseInfo.getPointerInfo();
        point = pointerInfo.getLocation();

        cX = (int) point.getX(); // get current mouse pointer's x-coord
        cY = (int) point.getY(); // get current mouse pointer's y-coord

        System.out.println("Current: (" + cX + ", " + cY + ")");

        System.out.println("Destination: (" + dX + ", " + dY + ")");

        while (cX != dX && cY != dY) { // move loop: move mouse pointer until at predetermined point
            if (cX < dX) {
                cX++;
            } else if (cX > dX) {
                cX--;
            } else {
            }

            if (cY < dY) {
                cY++;
            } else if (cY > dY) {
                cY--;
            } else {
            }

            robot.mouseMove(cX, cY); // move mouse pointer to next coordinate

            System.out.println("Current: (" + cX + ", " + cY + ")");

            try {
                Thread.sleep((int) (Math.random() * ((15 - 1) + 1)) + 1);
            } catch (InterruptedException ex) {
            }
        }
    }
}

Main Class: 主类:

import java.awt.AWTException;

public class Main {
    public static void main(String[] args) {
        try {
            new Thread(new MouseMover(200, 200)).start();
        } catch (AWTException ex) {
            ex.printStackTrace();
        }
    }
}

The issue is that the program only moves the mouse to one of the coordinates (whichever it reaches first) and immediately after stops. 问题是该程序仅将鼠标移动到坐标之一(无论它先到达哪个坐标),并在停止后立即移动。 I need it to shift the mouse to the correct coordinate. 我需要将鼠标移至正确的坐标。

Then why do you stop it when any coordinate reaches the destination: cX != dX && cY != dY ? 那么,为什么在任何坐标到达目标时停止它: cX != dX && cY != dY You should be using || 您应该使用|| instead of && . 代替&&

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM