请原谅我对linux操作系统/硬件问题的无知……我只是一个程序员:)
我有一个应用程序调用一些bash脚本来启动外部应用程序,在这种情况下Firefox。 该应用程序运行在具有触摸屏function的自助服务terminal上。 启动Firefox时,我还启动了一个虚拟键盘应用程序,允许用户input键盘。
但是,信息亭也有PS / 2和USB插槽,可以让用户插入键盘。 如果插入了键盘,如果我不需要启动虚拟键盘并为Firefox窗口提供更多的屏幕空间,那将会很好。
有没有办法让我检测是否从bash脚本中插入了键盘? 它会显示在/ dev中,如果是,它会显示在一个一致的位置? 如果用户使用PS / 2或USB键盘,它会有所作为吗?
谢谢!
对于USB设备,您可以使用lsusb并通过键盘协议(接口协议1)search人机界面设备(接口类3),例如
$ lsusb -v ... loads of stuff deleted ... Interface Descriptor: bLength 9 bDescriptorType 4 bInterfaceNumber 0 bAlternateSetting 0 bNumEndpoints 1 bInterfaceClass 3 Human Interface Device bInterfaceSubClass 1 Boot Interface Subclass bInterfaceProtocol 1 Keyboard iInterface 0 ... loads of stuff deleted ...
另外,你可以让udev帮助你。 列出/dev/input/by-path/的设备,键盘设备以-kdb (至less在udev规则中指定的)
$ ls -l /dev/input/by-path/*-kbd lrwxrwxrwx 1 root root 9 2010-03-25 09:14 /dev/input/by-path/pci-0000:00:1a.2-usb-0:1:1.0-event-kbd -> ../event4 $ ls -l /dev/input/by-path/*-kbd lrwxrwxrwx 1 root root 9 2009-08-29 09:46 /dev/input/by-path/platform-i8042-serio-0-event-kbd -> ../event1
对于USB键盘,我通常通过简单地search“lsusb -v”输出来查找键盘设备的单词“键盘”:
lsusb -v 2>/dev/null | egrep '(^Bus|Keyboard)' | grep -B1 Keyboard
示例输出:
Bus 001 Device 004: ID 413c:2006 Dell Computer Corp. bInterfaceProtocol 1 Keyboard
一个更通用的方法是search任何既是bInterfaceClass 03又bInterfaceProtocol 01的设备的/ sys / bus。由于你必须检测非USB设备,并且你想在脚本中使用输出,这种方法可能会更好为你:
grep -l 03 /sys/bus/*/*/*/bInterfaceClass | sed 's/Class$/Protocol/' | xargs grep -l 01 | xargs dirname
示例输出:
/sys/bus/usb/devices/1-4.1:1.0
警告:我找不到PS / 2键盘来testing这个脚本。 由于这个线程已经超过七年了,我猜测原来的问题的作者已经有了很长一段时间,并且不再需要PS / 2的检测。 我会留下这个答案,希望别人可能会觉得有用,但是请注意,我还没有使用非USB设备进行testing。
一种方法是做到这一点:
dmesg | grep keyboard
你也可以使用Upstart和udev来检测和操作键盘。
对于USB,您可以search/ sys / bus / usb / devices,查找具有带HID类(0x03)和协议键盘(0x01)的接口的设备。
Bash脚本:
#!/bin/bash for dev in /sys/bus/usb/devices/*-*:* do if [ -f $dev/bInterfaceClass ] then if [[ "$(cat $dev/bInterfaceClass)" == "03" && "$(cat $dev/bInterfaceProtocol)" == "01" ]] then echo "Keyboard detected: $dev" fi fi done