【发布时间】:2017-02-15 21:17:43
【问题描述】:
我正在尝试从 /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;
}
【问题讨论】:
-
我们有类似 libinput 的东西吗?一个例子是 X 的一个库,它可以完成所有这些。您可以查看它的来源。
-
您是否考虑过使用 SDL2 进行鼠标输入?
-
我最近发现我一直在寻找的是如何使用输入子系统来获取鼠标事件。我的问题解决了。无论如何,感谢您的帮助。
标签: c mouseevent mousewheel scrollwheel