首页 > 应用开发 > > linux下获取usb键盘按键键值
注意一下,USB键盘到底挂载到哪个event下即可( /dev/input/event0)
比较方便的方法是用: cat /dev/input/eventX ,没有返回错误就说明有这个设备,按一下按键有回应就是它了
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h>
struct input_event buff;
int fd;
int read_nu;
int main(int argc, char *argv[])
{
fd = open("/dev/input/event0", O_RDONLY);
if (fd < 0)
{
perror("can not open device usbkeyboard!");
return;
}
int i = 0;
printf("--fd:%d--\n",fd);
while(1)
{
while(read(fd,&buff,sizeof(struct input_event))==0)
{
;
}
if(buff.type == 1)
{
printf("type:%d code:%d value:%d\n",buff.type,buff.code,buff.value);
}
}
close(fd);
return 1;
}code:表示的是键值码。
type:0,表示是键盘,1表示是鼠标,和其他
value:1表示按下,0表示没有按下,2表示一直按下
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1976/viewspace-3143405/ ,如需转载,请注明出处,否则将追究法律责任。