我的应用程序的主要目标是能够从我的本地网络控制我的计算机以查看Netflix等。
我创建了一个非常好用的websocket。目前,我将实际鼠标移动推迟到名为nircmd的应用程序。
我有时会使用nircmd获取小型的hickup。所以我想使用awt.robot代替,但当我尝试使用awt.robot移动我的鼠标时,我收到以下错误: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编写的。
答案 0 :(得分:0)
请尝试通过代码禁用无头环境:
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也是如此。
在鼠标类中,您可以删除所有不必要的变量(gravity,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 + "\r\n"); //write the message to output stream
printWriter.flush();
Log.d("srv", "Sent: " + messsage);
} catch (IOException e) {
e.printStackTrace();
}
}
}