我有一个键盘插入一个Linux的盒子,然后我通过SSH运行我的Java。 我想知道是否有办法告诉Java来监听来自特定键盘/terminal的input。 由于我想捕获的键盘被插入到物理机器中,并且没有用户login,我不确定是否有办法做到这一点,但我想我可能会问这里?
如果您拥有root权限,则可以直接从/dev/input下的键盘设备读取键盘事件。 解码事件需要一点努力,但可以完成; 你可以在/ dev / input键盘格式中阅读数据格式 。
这个片段读取键盘事件,并识别您按下并释放哪个关键字AZ:
// replace path with path from your system DataInputStream in = new DataInputStream( new FileInputStream("/dev/input/by-id/usb-0430_0005-event-kbd")); String map = " abcdefghijlkmnopqrstuvwxyz "; // sizeof(struct timeval) = 16 byte[] timeval = new byte[16]; short type, code; int value; while (true) { in.readFully(timeval); type = in.readShort(); code = in.readShort(); value = in.readInt(); System.out.printf("%04x %04x %08x %c\n", type, code, value, map.charAt(value>>>24)); }