简体   繁体   English

Java WebSockets和awt.robot鼠标移动

[英]Java WebSockets and awt.robot Mouse Movements

The main goal of my application is to be able to control my computer from my local network for Netflix viewing and such. 我的应用程序的主要目标是能够从本地网络控制我的计算机以进行Netflix观看等操作。

I created a websocket that works really well. 我创建了一个非常有效的websocket。 Currently I'm deferring the actual mouse movements to an application called nircmd. 当前,我将实际的鼠标移动延迟到名为nircmd的应用程序中。

I sometimes get small hickups using nircmd. 有时我会使用nircmd获得一些小技巧。 So I would like to use an awt.robot instead, but when I try using awt.robot to move my mouse I get the following error: java.awt.AWTException: headless environment 因此,我想改用awt.robot,但是当我尝试使用awt.robot移动鼠标时,出现以下错误: java.awt.AWTException: headless environment

I went into the Administrator console in glass fish and found a property: -Djava.awt.headless=true Setting this to false produces the same error. 我进入玻璃鱼的管理员控制台,发现一个属性: -Djava.awt.headless=true将此设置为false会产生相同的错误。 Which boggles me. 这让我感到困惑。

Is it possible to give glass fish access to at least my mouse cursor and keyboard? 是否可以让玻璃鱼至少访问我的鼠标光标和键盘?

My server code is: 我的服务器代码是:

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

The client is written in javascript. 客户端是用javascript编写的。

Please try to disable headless environment by code: 请尝试通过代码禁用无头环境:

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

Later edit: 以后编辑:

I found and old project of mine in which i did this exact thing from an android device using the accelerometer, change it around a bit and it will fit you just fine: 我发现了我的一个旧项目,在该项目中,我使用加速度计从一个android设备上做了这个确切的事情,对其进行了一些更改,它将完全适合您:

Server class: 服务器类:

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

Mouse robot class: 鼠标机器人类:

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

}

The Server class first wait for a connection, after the connection is successful it starts listening for messages. Server类首先等待连接,连接成功后,它将开始侦听消息。 Each message that begins with X moves the mouse on the X axis, the same goes for Y. 每条以X开头的消息都会在X轴上移动鼠标,Y也是如此。

In the mouse class you could just remove all the unnecessary variables (gravity, maxAcc ...) and simply move the mouse to X / Y 在鼠标类中,您只需删除所有不必要的变量(重力,maxAcc ...),然后将鼠标移至X / Y

Sorry for the late reply: here you go: 抱歉,您的回复很晚:您可以继续:

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 class: 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 + "\r\n");  //write the message to output stream
            printWriter.flush();
            Log.d("srv", "Sent: " + messsage);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

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

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