#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h>
#include <unistd.h>
#include <time.h>
typedef struct input_event input;
#define MY_EVENT "/dev/input/event4"
int main() {
int fd = -1;
int ret = -1;
struct input_event ev = { 0 };
const int size = sizeof(input);
fd = open(MY_EVENT, O_RDONLY);
if (fd < 0) {
perror("open");
return -1;
}
while (1) {
ret = read(fd, &ev, size);
if (ret != size) {
close(fd);
return -1;
}
printf("type = %x, code = %x, value = %x\n", ev.type, ev.code,
ev.value);
struct tm t;
char date_time[64] = { 0 };
strftime(date_time, sizeof(date_time), "%Y-%m-%d %H:%M:%S",
localtime_r(&ev.time.tv_sec, &t));
printf("gettimeofday: date_time = %s\n", date_time);
}
close(fd);
}
其中 #define MY_EVENT “/dev/input/event4” 需要修改为你的设备对应的文件。