【问题标题】:select() isnt responding to writing on /dev/input/mice [duplicate]select() 没有响应 /dev/input/mice 上的写入 [重复]
【发布时间】:2012-09-19 03:19:51
【问题描述】:

可能重复:
select() isn't responding to writing on /dev/input/mice

我正在编写一个程序,它通过select() 监视键盘和鼠标设备文件。它等待对这些文件的任何写操作(这应该在有击键或鼠标移动时发生),并且一旦有写操作,就会执行一些作业。但它不起作用。代码如下。有人可以帮我吗?

#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");
        }


    if(mouse_fd>kbd_fd)
        {
            fd_max=mouse_fd;
        }
    else
        {
         fd_max=kbd_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);

        if(FD_ISSET(mouse_fd,&rfs))
            {
                printf("test mouse \n");
            }
        if(FD_ISSET(kbd_fd,&rfs))
            {
                printf("test keyboard \n");
                }

   }
}

当我执行程序时,它会给出如下输出:

[root@localhost Project]# gcc select.c
[root@localhost Project]# ./a.out
opening mouse device file has been successfull 
opening keyboard device file has been successfull 
test keyboard 
test mouse 
test keyboard 
test mouse 
test keyboard 
test mouse 
test keyboard 
test mouse 
test keyboard 
test mouse 

虽然我没有按任何键或移动鼠标。

【问题讨论】:

  • 您正在打开输入设备进行写入,并且您正在等待 select() 中的读取可能性。也许您应该打开这些设备进行阅读?

标签: linux unix io linux-device-driver device-driver


【解决方案1】:

你打开了我认为错误的文件

/dev/input/event3 - 键盘事件 /dev/input/event2 - 鼠标事件

检查 猫 /dev/input/event3

【讨论】:

    猜你喜欢
    • 2012-09-19
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多