您的位置:首页 > 运维架构 > Linux

Linux C中读取/dev/input/event设备来判断键盘按键是否按下

2015-11-03 17:24 423 查看
////

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: