Java WebSockets和awt.机器人鼠标运动



我的应用程序的主要目标是能够从我的本地网络控制我的计算机观看Netflix等。

我创建了一个工作得很好的websocket。目前,我将实际的鼠标移动延迟到一个名为nircmd的应用程序。

使用nircmd时,我有时会遇到小问题。所以我想用awt。但是当我尝试使用awt时。机器人移动我的鼠标我得到以下错误:java.awt.AWTException: headless environment

我进入了玻璃鱼的管理员控制台,发现了一个属性:-Djava.awt.headless=true将此设置为false会产生相同的错误。这让我很困惑。

是否有可能给玻璃鱼访问至少我的鼠标光标和键盘?

我的服务器代码是:
@OnMessage
public void onMessage(String message, Session sesh){
    Robot rob = null;
    try {
        rob = new Robot();
        rob.mouseMove(100, 100);
    } catch (AWTException ex) {
        Logger.getLogger(WebSocketServer.class.getName()).log(Level.SEVERE, null, ex);
    }

客户端使用javascript编写。

请尝试通过代码禁用无头环境:

static {
        System.setProperty("java.awt.headless", "false");
}

后来编辑:

我发现我的一个老项目,我做了这个确切的事情从一个android设备使用加速度计,改变它周围一点,它会适合你就好:

服务器

类:

public class Server {
    static ServerSocket server;
    static Socket s;
    private static InputStreamReader inputStreamReader;
    private static BufferedReader bufferedReader;
    public static void main(String[] args) throws IOException, SQLException,
            ClassNotFoundException, AWTException {
        server = new ServerSocket(4444);
        System.out.println("Waiting for clients to connect...");
        MouseRobot mr = new MouseRobot();
        while (true) {
            s = server.accept();
            InetAddress clientAddress = s.getInetAddress();
            System.out.println("Incoming connection from " + clientAddress);
            inputStreamReader = new InputStreamReader(s.getInputStream());
            bufferedReader = new BufferedReader(inputStreamReader); //get client msg       
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                System.out.println(line); 
                if (line.charAt(0) == 'X') {
                    mr.moveMouseX(Double.parseDouble(line.substring(1, line.length())));
                }
                else if (line.charAt(0) == 'Y') {
                    mr.moveMouseY(Double.parseDouble(line.substring(1, line.length())));
                }
            }
        }
    }   
}

鼠标机器人类:

public class MouseRobot {
    Robot robot;
    double width;
    double height;
    float gravity = 9.8f;
    float maxAcc = 19.6f;
    int lastX = 0;
    int lastY = 0;
    public MouseRobot() throws AWTException {
        robot = new Robot();
        robot.setAutoDelay(0);
        robot.setAutoWaitForIdle(false);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        width = screenSize.getWidth();
        height = screenSize.getHeight();
    }
    public void moveMouseX(double x) {
        x += gravity;
        robot.mouseMove((int) (width - x * width/maxAcc), lastY);
        lastX = (int) (width - x * width/maxAcc);
    }
    public void moveMouseY(double y) {
        y += gravity;
        robot.mouseMove(lastX, (int)(height - y * height/maxAcc));
        lastY = (int)(height - y * height/maxAcc);
    }
}

Server类首先等待连接,连接成功后开始侦听消息。每条以X开头的消息都会在X轴上移动鼠标,y也是如此。

在鼠标类中,你可以删除所有不必要的变量(重力,maxAcc…),并简单地将鼠标移动到X/Y

很抱歉回复晚了:给你:

MainActivity:

public class MainActivity extends Activity implements SensorEventListener{
    private SensorManager senSensorManager;
    private Sensor senAccelerometer;
    static EditText ipEditText;
    static TextView statusTextView;
    static Button connectBtn;
    static EditText messagesEditText;
    static TextView xTextView;
    static TextView yTextView;
    static int status = -1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //UI
        ipEditText = (EditText) findViewById(R.id.ipEditText);
        statusTextView = (TextView) findViewById(R.id.statusTextView);
        connectBtn = (Button) findViewById(R.id.connectBtn);
        messagesEditText = (EditText) findViewById(R.id.messagesEditText);
        xTextView = (TextView) findViewById(R.id.xTextView);
        yTextView = (TextView) findViewById(R.id.yTextView);
        //Sensors
        senSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        senAccelerometer = senSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        senSensorManager.registerListener(this, senAccelerometer, SensorManager.SENSOR_DELAY_FASTEST);
        connectBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setStatus(0);
                status = ServerClient.connectToServer(ipEditText.getText().toString());
                setStatus(status);
                if (status == 1) {
                    messagesEditText.setText("Successfully connected to: " + ipEditText.getText()
                        + "n-----------");
                }
            }
        });
    }
    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        if (status != 1)
            return;
        Sensor mySensor = sensorEvent.sensor;
        if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            ServerClient.sendMessage("x" + sensorEvent.values[0]);
            ServerClient.sendMessage("y" + sensorEvent.values[1]);
            messagesEditText.setText(messagesEditText.getText().toString());
            xTextView.setText("x: " + sensorEvent.values[0]);
            yTextView.setText("y: " + sensorEvent.values[1]);
            //ServerClient.sendMessage("z = " + sensorEvent.values[2]);
        }
    }
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }
    public static void setStatus(int status) {
        switch (status) {
            case -1:
                statusTextView.setText("Disconnected");
                statusTextView.setTextColor(Color.RED);
            case 0:
                statusTextView.setText("Connecting");
                statusTextView.setTextColor(Color.YELLOW);
            case 1:
                statusTextView.setText("Connected");
                statusTextView.setTextColor(Color.GREEN);
        }
    }
}

ServerClient类:

public class ServerClient {
    static Socket socket = null;
    static PrintWriter printWriter = null;
    public static int connectToServer(final String serverIP) {
        Thread connectionThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    try {
                        Log.d("srv", "trying to build socket");
                        socket = new Socket(serverIP, 4444); //connect to server
                        Log.d("srv", "socket made");
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                        Log.d("srv", "UnknownHostException");
                    } catch (IOException e) {
                        e.printStackTrace();
                        Log.d("srv", "IOException");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        connectionThread.start();
        try {
            connectionThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if(socket != null && socket.isConnected() && !socket.isClosed())
            return 1;
        else return -1;
    }
    public static void sendMessage(String messsage) {
        try {
            //dataOutputStream.writeUTF(messsage);
            printWriter = new PrintWriter(socket.getOutputStream(), true);
            printWriter.write(messsage + "rn");  //write the message to output stream
            printWriter.flush();
            Log.d("srv", "Sent: " + messsage);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新