你如何从 /dev/input/mice 读取鼠标按钮状态?我想检测按钮是否被按下。
您可以打开设备并从中读取。来自 /dev/input/mice 的事件有 3 个字节长,需要一些解析。我认为现在首选的方法是使用 /dev/input/event# 代替。但是,这是一个使用 /dev/input/mice 的小例子。
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char** argv)
{
int fd, bytes;
unsigned char data[3];
const char *pDevice = "/dev/input/mice";
// Open Mouse
fd = open(pDevice, O_RDWR);
if(fd == -1)
{
printf("ERROR Opening %s\n", pDevice);
return -1;
}
int left, middle, right;
signed char x, y;
while(1)
{
// Read Mouse
bytes = read(fd, data, sizeof(data));
if(bytes > 0)
{
left = data[0] & 0x1;
right = data[0] & 0x2;
middle = data[0] & 0x4;
x = data[1];
y = data[2];
printf("x=%d, y=%d, left=%d, middle=%d, right=%d\n", x, y, left, middle, right);
}
}
return 0;
}
单击鼠标生成:
x=0, y=0, left=1, middle=0, right=0
x=0, y=0, left=0, middle=0, right=0
一次鼠标移动(注意“相对”鼠标移动坐标):
x=1, y=1, left=0, middle=0, right=0
与@JustinB 建议的类似应用 - 但是在
python
import struct
with open("/dev/input/mice", mode="rb") as f:
while True:
button, x, y = struct.unpack('BBB', f.read(3))
print('x={:3}, y= {:3}, button= {:08b}'.format(x, y, button))