您可以打开设备并从中读取. / dev / input / mice中的事件长度为3个字节,需要进行一些解析.我认为现在首选的方法是使用/ dev / input / event#.但是,这是一个使用/ dev / input / mice的小例子.
#include
#include
#include
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