zoukankan      html  css  js  c++  java
  • 监听键盘

    /*keyboardview.c*/
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <linux/input.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    int main ()
    {
        int keys_fd;
        int pipes[2];
        struct input_event t;
        char buf[100];
        int pid,pid1;
        int nread;
        int key_code;
        if(pipe(pipes) < 0)
        {
            printf("%s
    ","pipes error!" );
            exit(EXIT_FAILURE);
        }
        pid = fork();
        if(pid < 0 )
        {
            printf("%s
    ","fork() function error!" );;
            exit(EXIT_FAILURE);
        }
        else if(pid == 0)
        {
            keys_fd = open ("/dev/input/event4", O_RDONLY);
            if (keys_fd <= 0)
            {
                printf ("open /dev/input/event4 device error!
    ");
            }
            close(pipes[0]);
            while (1)
            {
                if (read (keys_fd, &t, sizeof (t)) == sizeof (t))
                {
                    if (t.type == EV_KEY)
                    {
                        if ( t.value == 0)
                        {
                            printf ("key %d 
    ", t.code);
                            //sprintf(buf,"key %d 
    ", t.code);
                            key_code = t.code;
                            write(pipes[1],&key_code,sizeof(key_code));
                            if(t.code==KEY_ESC)
                                break;
                        }
                    }
                }
            }
            close (keys_fd);
            close(pipes[1]);
            exit(EXIT_SUCCESS);
        }
        else
        {
            pid1 = fork();
            if(pid1 < 0 )
            {
                printf("%s
    "," fork function error" );
                exit(EXIT_FAILURE);
            }
            if(pid1 == 0)
            {
                FILE * fp;
                close(pipes[1]);
                //int writefd;
                fp = fopen("data.out","w+");
                //writefd = open("data.out", O_WRONLY | O_CREAT);
                while(1)
                {
                    read(pipes[0],&key_code,sizeof(key_code));
                    //write(writefd,&key_code,sizeof(key_code));
                    fprintf( fp,"%d    ",key_code);
                    if(key_code == 1)
                        break;
                }
                close(pipes[0]);
                exit(EXIT_SUCCESS);
            }
            else
            {
                exit(EXIT_SUCCESS);
            }         
        } 
        return 0;
    }
  • 相关阅读:
    iterm2 配色修改
    让Dock自动 显示/隐藏 不再有延迟
    Xcode更改配色方案
    CocoaPods安装与使用
    CocoaPods安装和使用及问题:Setting up CocoaPods master repo
    CocoaPods安装和使用教程
    RubyGems 镜像
    iOS Mac系统下Ruby环境安装
    MAC机中安装RUBY环境
    Kibana+Logstash+Elasticsearch 日志查询系统
  • 原文地址:https://www.cnblogs.com/yongjiuzhizhen/p/3419957.html
Copyright © 2011-2022 走看看