美文网首页LinuxdeepinLinux学习|Gentoo/Arch/FreeBSD
Linux通过读取设备文件监听键盘

Linux通过读取设备文件监听键盘

作者: Daryl_Xu | 来源:发表于2018-06-16 16:37 被阅读24次
    #include <stdio.h>
    #include <linux/input.h>
    #include <fcntl.h>
    #include <unistd.h>
    
    #define DEV_PATH "/dev/input/event5"   //根据需要修改为你的键盘对应的设备文件,可以直接sudo cat /dev/input/event5并按键盘,看是否有输出,见附图
    
    int main()
    {
        int keys_fd;  //文件标志
        char ret[2];
        struct input_event t;  //读取到的input设备数据是一个结构体
        
        keys_fd = open(DEV_PATH, O_RDONLY);  //权限不通过的时候一般会返回-1
        if(keys_fd <= 0)
        {
            printf("open /dev/input/event2 device error!\n");
            return -1;
        }
        while(1)
        {
            if(
                read(keys_fd, &t, sizeof(t)) == sizeof(t)
                )  /*keys_fd指向打开的设备文件,read将从设备文件传送sizeof(t)个字节的数据到&t这个内存地址。函数执行顺利的话返回值是实际读取的字节数*/
            {
                if(t.type == EV_KEY)
                {
                    if(t.value==0 || t.value==1)
                    {
                        printf("key %d %s\n", t.code, (t.value) ? "Pressed" : "Released");  //t.code值所对应的按键在/usr/include/linux/input-event-codes.h可以查到  
                    }
                }
            }
        }
        close(keys_fd);
        
        return 0;
    }
    
    附图
    操作其他的设备类似,不过要提前了解相关的数据结构
    但是这种监听是需要有设备文件的读取权限的(默认root用户才有),循环读取设备文件似乎开销比较大?
    推荐Linux全局事件监听技术

    相关文章

      网友评论

        本文标题:Linux通过读取设备文件监听键盘

        本文链接:https://www.haomeiwen.com/subject/olebeftx.html