#include <stdio.h>
#include <linux/input.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define DEV_PATH "/dev/input/event4" //difference is possible
int main(int argc, char *argv[])
{
int keys_fd;
char ret[2];
struct input_event t;
if(argc <2)
{
printf("input device error\n");
return -1;
}
keys_fd=open(DEV_PATH, O_RDONLY);
if(keys_fd <= 0)
{
printf("open /dev/input/event2 device error!\n");
return -1;
}
while(1)
{
if(read(keys_fd, &t, sizeof(t)) == sizeof(t))
{
if(t.type==EV_KEY )
// if(t.type==EV_MSC )
//if(t.value==0 || t.value==1)
if( t.value==1)
{
printf("key %d \n", t.code);
if(t.code == KEY_ESC)
break;
}
}
}
close(keys_fd);
return 0;
}
Linux读取按键
最新推荐文章于 2023-08-20 11:41:37 发布
本文提供了一个简单的C程序示例,展示了如何在Linux环境下读取特定输入设备(如键盘)的事件。通过使用标准的Linux输入API,程序能够识别按键事件并打印按键代码,特别针对ESC键设置退出条件。
484

被折叠的 条评论
为什么被折叠?



