我需要一些方法来检测Linux上的鼠标/键盘活动。我需要记录此活动并使用tcp socket将此记录发送到我的Android平板电脑。我在终端中运行这个程序,它在线程“main”中显示错误Exception java.lang.UnsupportedClassVersionError:Mouse:Unsupported major.minor version 51.0..any help ????
import java.awt.HeadlessException;
import java.awt.MouseInfo;
import java.awt.Point;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Timer;
import java.util.TimerTask;
public class Mouse {
public static void main(String[] args) throws InterruptedException {
Point p, prev_p;
p = MouseInfo.getPointerInfo().getLocation();
DatagramSocket socket = null;
try {
socket = new DatagramSocket(8988);
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InetAddress addr = null;
try {
addr = InetAddress.getByName("107.108.203.204");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
File file = new File("/sys/kernel/debug/usb/usbmon/6u");
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
System.err
.println("To fix the error Run as root or Change ownership of the file to the user who runs this program");
}
String line, s = null;
try {
while ((line = br.readLine()) != null) {
prev_p = p;
p = MouseInfo.getPointerInfo().getLocation();
String[] arr = line.split(" ");
if (arr.length == 8)
s = arr[7];
System.out.println(s+" "+Integer.parseInt(s.substring(2,4),16));
byte[] buffer = s.getBytes();
DatagramPacket pak = new DatagramPacket(buffer, buffer.length,
addr, 8988);
try {
socket.send(pak);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
答案 0 :(得分:1)
我不会详细介绍如何使用tcp套接字,这很简单。
然而,您的问题的基础是您需要打开并不断读取/ dev / input / by-id / yourmouseorkeyboardnamehere文件。读取此文件将导致程序阻塞,直到有键盘/鼠标输入(取决于您是否读取键盘或鼠标文件),然后您将能够读取表示来自键盘或鼠标的数据的数据。
从那里通过tcp套接字向平板电脑发送这些数据相当容易,您可以从互联网上的任何套接字教程中学习。
如果您有任何疑问或需要更多详细信息,请发表评论。