Linux C 키 코드 읽 기

Linux 에서 C 코드 로 버튼 이 벤트 를 읽 는 key code
#include <stdio.h>
#include <stdlib.h>
#include <linux/input.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/time.h>


struct timeval begin_time;
int reboot_done  = 0;

static void show_event(struct input_event* event)
{
    int current_time = event->time.tv_sec * 1000 + (int) event->time.tv_usec/1000;

    {
        if( event->value == 0 )
        {
            begin_time.tv_sec = 0;
            begin_time.tv_usec = 0;
            reboot_done  = 0;
                printf("%d %d %d [%d.%d]
", event->type, event->code, event->value, event->time.tv_sec,(int) event->time.tv_usec/1000 ); printf("press release !
"); } else if( event->value == 1 ) { begin_time.tv_sec = event->time.tv_sec; begin_time.tv_usec = event->time.tv_usec; printf("%d %d %d [%d.%d]
", event->type, event->code, event->value, event->time.tv_sec,(int) event->time.tv_usec/1000 ); } else if( event->value == 2 ) { if(event->time.tv_sec - begin_time.tv_sec >= 4) { if(event->time.tv_usec - begin_time.tv_usec >= 0) { if(reboot_done == 0) { printf("%d %d %d [%d.%d]
", event->type, event->code, event->value, event->time.tv_sec,(int) event->time.tv_usec/1000 ); reboot_done = 1; printf("press long then 4 s
"); } } } } } } int main(int argc, char* argv[]) { struct input_event event = {{0}, 0}; const char* file_name = argc == 2 ? argv[1] : "/dev/input/event0"; int fd = open(file_name, O_RDWR); if(fd > 0) { do { int ret = read(fd, &event, sizeof(event)); if(ret == sizeof(event)) { show_event(&event); } } while(1); close(fd); } else printf("open filed %s
", argv[1]); return 0; }

좋은 웹페이지 즐겨찾기