日期:2014-05-16  浏览次数:20795 次

uClinux下应用程序获取USB键值

[ 注:内核代码中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=0110
N: Name="Logitech USB Keyboard"
P: Phys=usb-musb-hdrc-1/input0
S: Sysfs=/devices/platform/musb-blackfin.0/musb-hdrc/usb1/1-1/1-1:1.0/input/input0
U: Uniq=
H: Handlers=kbd event0
B: PROP=0
B: EV=120013
B: KEY=10000 7 ff9f207a c14057ff febeffdf ffefffff ffffffff fffffffe
B: MSC=10
B: LED=1f

I: Bus=0003 Vendor=046d Product=c31d Version=0110
N: Name="Logitech USB Keyboard"
P: Phys=usb-musb-hdrc-1/input1
S: Sysfs=/devices/platform/musb-blackfin.0/musb-hdrc/usb1/1-1/1-1:1.1/input/input1
U: Uniq=
H: Handlers=kbd event1
B: PROP=0
B: EV=1b
B: KEY=2010000 397a d801d001 1e0000 0 0 0
B: ABS=1 0
B: 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;
}