我正在尝试从/dev/input/mice文件中读取鼠标事件。我能够解析 3 字节鼠标输入以获取三个按钮状态以及 X 和 Y 坐标的增量。但是,向上滚动时的鼠标输入与向下滚动时的鼠标输入相同。如何区分向上滚动事件和向下滚动事件?是否有任何ioctl可以进行任何所需的配置,以便我在这两个事件上从鼠标获得不同的输入?
下面是一个简单的程序,用于在鼠标事件发生时查看鼠标的输入。向上滚动和向下滚动事件导致该程序打印相同的输出(即 8 0 0)。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
int main(void) {
int mouse_fd = open("/dev/input/mice", O_RDONLY | O_NONBLOCK);
signed char input[4];
ssize_t rd_cnt;
if(mouse_fd < 0)
{
perror("Could not open /dev/input/mice");
exit(EXIT_FAILURE);
}
while(true)
{
errno = 0;
rd_cnt = read(mouse_fd, input, 4);
if(rd_cnt <= 0 && errno != EAGAIN)
{
perror("Mouse read error:");
exit(EXIT_FAILURE);
}
else
{
for(int i = 0; i < rd_cnt; i++)
{
printf("%d", input[i]);
if(i == rd_cnt - 1)
{
printf("\n");
}
else
{
printf("\t");
}
}
}
}
return 0;
}