Io select()对在/dev/input/mice上写入没有响应
我正在写一个程序,通过键盘上的选择和鼠标设备文件进行监控。它等待任何写入操作,当在这些文件上有击键或鼠标移动时,会发生这种情况,一旦有写入操作,就会执行一些作业 但它不起作用。我的代码如下Io select()对在/dev/input/mice上写入没有响应,io,device,c,input,Io,Device,C,Input,我正在写一个程序,通过键盘上的选择和鼠标设备文件进行监控。它等待任何写入操作,当在这些文件上有击键或鼠标移动时,会发生这种情况,一旦有写入操作,就会执行一些作业 但它不起作用。我的代码如下 #include<stdio.h> #include<string.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<linux/input.h&g
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<linux/input.h>
#include<linux/uinput.h>
#include<sys/time.h>
#include<unistd.h>
void main()
{
int mouse_fd,kbd_fd,fd_max;
struct input_event ev;
fd_set rfs,wfs;
if((mouse_fd=open("/dev/input/event3",O_WRONLY))==-1)
{
printf("opening mouse device file has failed \n");
}
else
{
printf("opening mouse device file has been successfull \n");
}
if((kbd_fd=open("/dev/input/event2",O_WRONLY))==-1)
{
printf("opening keyboard device file has failed \n");
}
else
{
printf("opening keyboard device file has been successfull \n");
}
FD_ZERO(&rfs);
FD_ZERO(&wfs);
FD_SET(mouse_fd,&rfs);
FD_SET(kbd_fd,&rfs);
FD_SET(mouse_fd,&wfs);
FD_SET(kbd_fd,&wfs);
if(mouse_fd>kbd_fd)
{
fd_max=mouse_fd;
}
else
{
fd_max=kbd_fd;
}
while(1)
{
select((fd_max+1),&rfs,NULL,NULL,NULL);
sleep(2);
if(FD_ISSET(mouse_fd,&rfs))
{
printf("test mouse \n");
}
if(FD_ISSET(kbd_fd,&rfs))
{
printf("test keyboard \n");
}
}
}
即使我没有按任何键。此外,select从不选择鼠标设备文件,即使存在鼠标的物理移动
我做错了什么?在每次select调用之前,您需要重新初始化fd集。因此,程序中的循环如下所示:
while(1)
{
FD_ZERO(&rfs);
FD_ZERO(&wfs);
FD_SET(mouse_fd, &rfs);
FD_SET(kbd_fd, &rfs);
FD_SET(mouse_fd, &wfs);
FD_SET(kbd_fd, &wfs);
select((fd_max+1),&rfs,NULL,NULL,NULL);
// proceed normally
}
此外,根据,您需要打开设备进行读取,因为您正在尝试从设备读取数据:
// Open device for reading (do you mean to use "/dev/input/mice" here?)
if ((mouse_fd = open("/dev/input/event3", O_RDONLY)) == -1)
Linux包括一个教程手册页,其中解释了如何使用select和一个示例程序,可以作为参考。Select Law 11提醒您需要在每次调用Select之前重新初始化fd集合。Mrb,我在一个程序中遇到了相同的问题,您对此的回答非常有用。即,或者只使用poll,而不需要每次都重新初始化。@mrb…刚刚按照你的建议做了…1.每次重新初始化fd_集&2.将鼠标设备文件的permission设置为O-RDONLY
// Open device for reading (do you mean to use "/dev/input/mice" here?)
if ((mouse_fd = open("/dev/input/event3", O_RDONLY)) == -1)