Linux C中读取/dev/input/event设备来判断键盘按键是否按下
2015-11-03 17:24
423 查看
////
{
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/event2 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;
}
include
include <linux/input.h>
include
include <sys/types.h>
include <sys/stat.h>
include
define DEV_PATH "/dev/input/event2" //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/event2 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;
}
相关文章推荐
- linux线程异常保护
- linux一天一命令博客链接
- linux下静态库的ranlib使用
- linux ar命令用法
- linux的crontab定时服务备份mysql数据
- Centos7 安装python3的独立环境
- Linux下使用Eclipse开发Hadoop应用程序
- Linux下crontab定时执行脚本
- java代码调用linux命令,生成.pem公钥私钥证书
- centos7 挂载virtualbox 中无法挂载共享文件
- PostgreSQL在CentOS下的源码安装
- 理解LInux路由
- 如何找出Linux系统中内置模块的信息
- centos7安装配置lnmp
- linux chmod 使用方法
- Linux CentOS增加swap分区
- Linux
- VMWare LINUX虚拟机设置固定IP上网方法
- 重新定位svn地址的方法(windows和linux),svn switch(sw)的帮助信息
- 如何理解ip路由和操作linux的路由表