文章目录
本文简单介绍几种在linux下查看键鼠输入的方法
通过打开input设备来读取输入
程序如下,使用时需要看情况修改input设备路径
c
#include <fcntl.h>
#include <linux/input.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#define DEV_PATH "/dev/input/event1" // difference is possible
int main()
{
int keys_fd;
char ret[2];
struct input_event t;
keys_fd = open(DEV_PATH, O_RDONLY);
if (keys_fd <= 0) {
printf("open /dev/input/event1 device error!\n");
return -1;
}
while (1) {
if (read(keys_fd, &t, sizeof(t)) == sizeof(t)) {
if (t.type == EV_KEY)
if (t.value == 0 || t.value == 1) {
printf("key %d %s\n", t.code, (t.value) ? "Pressed" : "Released");
if (t.code == KEY_ESC)
break;
}
}
}
close(keys_fd);
return 0;
}