keytest.c

来自「Linux s3c2410键盘驱动源码」· C语言 代码 · 共 57 行

C
57
字号
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

main()
{
   fd_set rfds;
   struct timeval tv;
   int retval;

   int fileno;
   int ts, maxfd;
   int  ret= 0;
   char val=0;

    fileno = open("/dev/key",O_RDONLY);
    if (fileno == -1) {
        printf("open device key error!\n");
        return 0;
    }
    printf("press '*' to exit\n");
    while(1)
    {
        FD_ZERO(&rfds);
        FD_SET(fileno, &rfds);
        /* Wait up to five seconds. */
        tv.tv_sec = 5;
        tv.tv_usec = 0;

        maxfd =  fileno;
        retval = select(maxfd+1, &rfds, NULL, NULL, &tv);
        /* Don't rely on the value of tv now! */
        if (retval)
        {
             if (FD_ISSET(fileno, &rfds))
             {
                 if (read(fileno, &val, 1) == 0)
                 {
                     if(val=='*'){
		        	printf("\n");	
			        goto exit;
		     }else       printf("Key %c pressed\n",val);
                 }
     	     }else   printf("read error\n");
        }

    }
exit:
        close(fileno);
        close(ts);
        return 0;

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?