uClinux下应用程序获取USB键值

来源:互联网 发布:php达内项目经理面试题 编辑:程序博客网 时间:2024/05/16 01:01

[ 注:内核代码中Documentation/input/input.txt,有输入设备的介绍。]

系统检测到USB键盘后,一般会将其映射到/dev/input/event0,可能使用cat命令进行确认:

      #cat /dev/input/event0

操作USB键盘时会有乱码出现。

也可以用以下命令查看设备与节点的关联:

      #cat /proc/bus/input/devices

我的系统接入一块罗技的USB键盘,输出以下信息:

I: Bus=0003 Vendor=046d Product=c31d Version=0110N: Name="Logitech USB Keyboard"P: Phys=usb-musb-hdrc-1/input0S: Sysfs=/devices/platform/musb-blackfin.0/musb-hdrc/usb1/1-1/1-1:1.0/input/input0U: Uniq=H: Handlers=kbd event0B: PROP=0B: EV=120013B: KEY=10000 7 ff9f207a c14057ff febeffdf ffefffff ffffffff fffffffeB: MSC=10B: LED=1fI: Bus=0003 Vendor=046d Product=c31d Version=0110N: Name="Logitech USB Keyboard"P: Phys=usb-musb-hdrc-1/input1S: Sysfs=/devices/platform/musb-blackfin.0/musb-hdrc/usb1/1-1/1-1:1.1/input/input1U: Uniq=H: Handlers=kbd event1B: PROP=0B: EV=1bB: KEY=2010000 397a d801d001 1e0000 0 0 0B: ABS=1 0B: MSC=10
应用程序可以用下面的程序来读取USB键值:
#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdio.h>#include <linux/input.h>struct input_event buf;int main(int argc, char **argv){    int fd;    int nread;    fd = open("/dev/input/event0", O_RDONLY);    if (fd < 0)    {        printf("fail to open usbdev.\n");        exit(1);    }    printf("--fd = %d--\n", fd);    while (1)    {        nread = read(fd, &buf, sizeof(buf));        if (nread != 0)        {            printf("type : %d, code = %d, value = %d\n",                    buf.type, buf.code, buf.value);        }    }    return 0;}


原创粉丝点击