如何在 linux 上捕获原始 HID 输入?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/873975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to capture raw HID input on linux?
提问by polyglot
Short version of what I want to accomplish: I have a foot pedal (INFINITY-IN-USB-1, if that's of any interest) that is implemented as a generic HID device, and I would like it to be behave as control and alt keys on linux. I'm looking for something on the level on X, i.e. not just individual programs.
我想要完成的简短版本:我有一个脚踏板(INFINITY-IN-USB-1,如果有兴趣的话),它是作为通用 HID 设备实现的,我希望它可以作为控制和 alt Linux 上的密钥。我正在寻找 X 级别上的东西,即不仅仅是单个程序。
Longer version: I have this setup working in Windows XP by calling dll RegisterRawInputDevices and GetRawInputData and listening to the input in a hidden windows. All is accomplished with a fairly straightforward Autohotkeyscript (can post here if anyone is interested). The point is, there is no need for any extra driver, etc.; only the native Windows dll's are needed.
更长的版本:我通过调用 dll RegisterRawInputDevices 和 GetRawInputData 并在隐藏窗口中侦听输入,使此设置在 Windows XP 中工作。所有这些都是通过一个相当简单的Autohotkey脚本完成的(如果有人感兴趣,可以在这里发布)。关键是,不需要任何额外的驱动程序等;只需要本机 Windows dll。
I want this setup to work on linux (more specifically Gnome under Ubuntu, but I do occasionally use other distro/window manager, so solution on the level of X is appreciated). This foot pedal does not register itself as keyboard or even joystick, but as a HID device with UsagePage 12, Usage 3. I notice that the latest Ubuntu can detect and use the favorite keys on the Microsoft Natural Keyboard, which also register as HID device*. I make use of those keys on Windows using the same method as I use the foot pedal (i.e. without the bundled software). So I assume that this pedal thing can work on linux as well, but the question is how?
我希望这个设置可以在 linux 上运行(更具体地说是 Ubuntu 下的 Gnome,但我偶尔会使用其他发行版/窗口管理器,因此对 X 级别的解决方案表示赞赏)。这个脚踏板不会将自己注册为键盘甚至操纵杆,而是作为一个 HID 设备,使用 UsagePage 12, Usage 3。我注意到最新的 Ubuntu 可以检测和使用 Microsoft Natural Keyboard 上的常用键,它也注册为 HID 设备*. 我使用与使用脚踏板相同的方法(即没有捆绑软件)在 Windows 上使用这些键。所以我假设这个踏板的东西也可以在 linux 上工作,但问题是如何?
[I'm new to the lower-level stuff on Linux, so please be generous with links to introduction or tutorial whenever applicable.]
[我对 Linux 上的低级内容不熟悉,因此请在适用时提供介绍或教程的链接。]
* For those not in the know, the Microsoft Natural Keyboard registers as both a regular keyboard and a generic HID device. Without some application (such as the bundled application) knowing how to interpret the HID input, the regular keys will work but the favorite keys and the special function keys are useless.
* 对于不知情的人,Microsoft Natural Keyboard 既可注册为常规键盘,也可注册为通用 HID 设备。如果某些应用程序(例如捆绑应用程序)不知道如何解释 HID 输入,则常规键可以使用,但收藏键和特殊功能键无用。
To clarify, linux is NOT seeing the pedal presses as input. It does recognize the USB device, but xev gives no output for pedal presses. I have seen reports of people succesefully using the pedal under WINE for transcription software, but that must work in a Windows way and does not give what I want (which is making the pedal look like a keyboard to all native linux apps). I wonder whether some low level linux keyboard driver is needed?
澄清一下,linux 没有将踏板按下视为输入。它确实识别了 USB 设备,但 xev 没有提供踏板压下的输出。我已经看到有人成功地将 WINE 下的踏板用于转录软件的报告,但这必须以 Windows 方式工作并且没有提供我想要的(这使得踏板看起来像所有原生 Linux 应用程序的键盘)。我想知道是否需要一些低级别的 linux 键盘驱动程序?
Here's some more information: If I issue this in the terminal
这里有一些更多信息:如果我在终端中发出这个
cat /dev/usb/hiddev0
(dev/usb/hiddev0
is my pedal), I get raw codes corresponding to my pedal presses. That's a start. I know what those raw code means: in binary, 0001, 0010, 0100 corresponds to each pedal, respectively, and combination of pedal presses sends combination of those binary number, and releases of pedal trigger input of whatever pedal still being pressed (if all pedal is up, 0000 is sent).
(dev/usb/hiddev0
是我的踏板),我得到与我的踏板压力对应的原始代码。那是一个开始。我知道那些原始代码是什么意思:在二进制中,0001、0010、0100 分别对应于每个踏板,踏板按下的组合发送这些二进制数的组合,并释放仍在按下的任何踏板的踏板触发器输入(如果全部踏板向上,发送 0000)。
How to I get X to listen to dev/usb/hiddev0
and translate the raw codes into maybe a special keycode so that I can map them with xmodmap or something?
如何让 X 收听dev/usb/hiddev0
并将原始代码翻译成一个特殊的键码,以便我可以用 xmodmap 或其他东西映射它们?
采纳答案by Adam Goode
You'll want uinput
. You'll listen on your /dev/usb/hiddev0 and then create new events that you'll send out on /dev/input/uinput
.
你会想要的uinput
。您将在 /dev/usb/hiddev0 上侦听,然后创建将在 上发送的新事件/dev/input/uinput
。
This explains it and gives a little tutorial: Using uinput driver in Linux- 2.6.x to send user input{This is the EInfochips' "Dashboard" publication issue January 2007 "Tip of the Month" article mentioned on thisarchived page}.
这对其进行了解释并提供了一个小教程: 在 Linux-2.6.x 中使用 uinput 驱动程序发送用户输入{这是 EInfochips 的“仪表板”出版期 2007 年 1 月“本月提示”文章在此存档页面上提到}。
回答by bitozoid
I use for my binding/shorcuts a combination of compiz, easystroke and xmacro.
我将 compiz、easystroke 和 xmacro 组合用于我的绑定/快捷方式。
For your needs, I think the missing piece is xbindkeys. I have found this link for you that maybe helps you to set this up:
根据您的需要,我认为缺少的部分是 xbindkeys。我为您找到了这个链接,可能可以帮助您进行设置:
http://linux-trackball.dreamhosters.com/
http://linux-trackball.dreamhosters.com/
I wonder anyway whether there is a way to distinguish between several mouse devices.
我想知道是否有办法区分几种鼠标设备。
回答by joeforker
You should investigate lircd. It interprets input from remote controls. Some supported remotes apparently present themselves as generic hid devices, so you may be able to get your device to talk to lircd.
你应该调查 lircd。它解释来自遥控器的输入。一些受支持的遥控器显然将自己显示为通用 hid 设备,因此您可以让您的设备与 lircd 通话。
回答by Alexey Feldgendler
Does the device show up in /dev/input? if it does, use the "evdev" X driver to hook it up just like you would do for a keyboard or mouse.
设备是否出现在 /dev/input 中?如果是,请使用“evdev”X 驱动程序将其连接起来,就像连接键盘或鼠标一样。
回答by kapace
Same problem here, but with special keys on a wireless keyboard. I feel your pain.
同样的问题,但无线键盘上有特殊键。我感觉到你的痛苦。
Anyways, trying to get this to work, heres my method:
无论如何,试图让它发挥作用,这是我的方法:
sleep 10; killall cat
then quickly in another terminal:cat /dev/usb/hiddevice0 > key1.usbdump
and press/use the device. This will dump the hiddevice's binary output for that key.- I quickly hacked together a python script to read the hiddevice input and fire events. So far it works for the first time the key is hit. This is similar to what Adam suggested, but I think uinput is more difficult to program/use although perhaps more elegant, and python is readily available.
sleep 10; killall cat
然后在另一个终端中快速:cat /dev/usb/hiddevice0 > key1.usbdump
并按下/使用该设备。这将转储该密钥的 hiddevice 二进制输出。- 我很快编写了一个 python 脚本来读取 hiddevice 输入和触发事件。到目前为止,它是第一次击中键。这类似于 Adam 的建议,但我认为 uinput 更难编程/使用,尽管可能更优雅,而且 python 很容易获得。
So this is a work in progress (only works for the first time pressed), but maybe something like this could work for you:
所以这是一项正在进行的工作(仅在第一次按下时有效),但也许这样的事情对你有用:
sf1 = open("test.usbdump").read() # read the earlier usb dump from hiddevice
kb = open("/dev/usb/hiddev0")
while 1:
# Prints true if the specific key has been read.
print (kb.read(len(sf1)) == sf1)
# Basically all that has to be done is if ^ is true, then fire off the event you want.
If anyone can help me out with my program or if I'm doing this wrong, please tell me. ;)
如果有人可以帮助我解决我的程序,或者我做错了,请告诉我。;)
I realise that there are some headers being included in the initial dump of the hiddevice. Using some bless hex editing and bit differencing, you could find which values are important and make a check for them in python. (For example, the hex digit "B0" means a special function key has been pressed on my keyboard, and later on there is more information on which key was pressed etc.)
我意识到 hiddevice 的初始转储中包含一些标头。使用一些祝福十六进制编辑和位差分,您可以找到哪些值很重要并在 python 中检查它们。(例如,十六进制数字“B0”表示在我的键盘上按下了一个特殊的功能键,稍后有关于按下哪个键等的更多信息。)
My end result is this: hiddevice0 seems to hang and stop giving data after a while, not sure why, but instead, I use /dev/input/event* (It might work for you too,) and that seems to work better. Again, same hexediting and low level parsing leads to success. Along the way I found that sudo cat /dev/input/event3 | hexdump
is extremely helpful in determining which bytes are important to you.
我的最终结果是: hiddevice0 似乎挂起并在一段时间后停止提供数据,不知道为什么,但相反,我使用 /dev/input/event* (它也可能对你有用),这似乎效果更好。同样,相同的十六进制编辑和低级解析会导致成功。在此过程中,我发现这sudo cat /dev/input/event3 | hexdump
对于确定哪些字节对您很重要非常有帮助。
So, if you have a Sk-8812 IBM keyboard, and would like to use the special keys, you can talk to me to get the script I used.
所以,如果您有一个 Sk-8812 IBM 键盘,并且想使用特殊键,您可以与我交谈以获取我使用的脚本。
回答by cristoper
I wrote about how I got my Infinity IN-USB-1 (AKA "VEC USB Footpedal") to send arbitrary keysyms under X on my weblog:
我写了我如何让我的 Infinity IN-USB-1(又名“VEC USB Footpedal”)在我的博客上发送 X 下的任意键符:
Use VEC/Infinity USB Foot Pedal as a Keyboard Under Linux
使用 VEC/Infinity USB 脚踏板作为 Linux 下的键盘
(It should also work for IN-USB-2 and maybe some other USB foot controller models sold by VEC and P.I. Engineering and their clones.)
(它也应该适用于 IN-USB-2 以及 VEC 和 PI Engineering 及其克隆出售的其他一些 USB 脚踏控制器型号。)
Here is the shorter version:
这是较短的版本:
Get X to recognize the pedal
让 X 识别踏板
The pedal is an HID-compliant USB device and the kernel has no problem discovering it and making its events available to userspace via a /dev/input/eventX
node. To see that you can run the evtest
program (on Debian: sudo apt install evtest
). So you don't needto go to the HID level to use the foot pedals.
踏板是符合 HID 的 USB 设备,内核可以毫无问题地发现它并通过/dev/input/eventX
节点将其事件提供给用户空间。要查看您是否可以运行该evtest
程序(在 Debian 上:)sudo apt install evtest
。因此,您无需进入 HID 级别即可使用脚踏板。
The problem is that udev does not tag it as a keyboard or mouse, so X ignores it. This can be fixed with a one-line udev rule file (thanks to Parlatypedeveloper Gabor Karsay for providing this solution in Parlatype Issue 28):
问题是 udev 没有将它标记为键盘或鼠标,所以 X 忽略了它。这可以通过一行 udev 规则文件解决(感谢Parlatype开发人员 Gabor Karsay 在Parlatype Issue 28 中提供此解决方案):
ACTION=="add|change", KERNEL=="event[0-9]*", ATTRS{idVendor}=="05f3", ATTRS{idProduct}=="00ff", ENV{ID_INPUT_KEYBOARD}="1"
Put that line in a file called /etc/udev/rules.d/10-vec-usb-footpedal.rules
. No need to restart anything, udev should automatically detect the file.
将该行放在名为/etc/udev/rules.d/10-vec-usb-footpedal.rules
. 无需重新启动任何东西,udev 应该会自动检测该文件。
Now when you unplug and replug the USB pedal, it should be recognized by X (and send mouse button events). To verify, run xev
.
现在,当您拔下并重新插入 USB 踏板时,它应该被 X 识别(并发送鼠标按钮事件)。要验证,请运行xev
.
Remapping keysyms with udev hwdb
使用 udev hwdb 重新映射键符
Having a pedal that sends only mouse clicks is probably not what you want. This key codes sent by the pedal switches can be remapped with a udev hwdb file.
拥有只发送鼠标点击的踏板可能不是您想要的。踏板开关发送的这个键码可以用 udev hwdb 文件重新映射。
Create a file under /etc/udev/hwdb.d/
(I put mine in /etc/udev/hwdb.d/60-usb-footpedal.hwdb
) containing these lines:
/etc/udev/hwdb.d/60-usb-footpedal.hwdb
在/etc/udev/hwdb.d/
(我把我的/etc/udev/hwdb.d/60-usb-footpedal.hwdb
)下创建一个包含这些行的文件:/etc/udev/hwdb.d/60-usb-footpedal.hwdb
evdev:input:b*v05F3p00FF*
KEYBOARD_KEY_90001=f14
KEYBOARD_KEY_90002=f15
KEYBOARD_KEY_90003=f16
This time we do need to inform the system to update the binary hwdb file:
这次我们确实需要通知系统更新二进制hwdb文件:
$ sudo systemd-hwdb update
And then unplug and repug the device.
然后拔下并重新插入设备。
The first line of the hwdb file matches our device (vendor 05F3
, product 00FF
), and the subsequent lines map a scancode (hex) to a keycode. I chose the F14, F15, and F16 function keys, but a list of available keycodes is defined in /usr/include/linux/input-event-codes.h; to use the names #defined in that file as hwdb keycodes, simply convert them to lowercase and remove the key_
prefix.
hwdb 文件的第一行与我们的设备(供应商05F3
、产品00FF
)匹配,随后的行将扫描码(十六进制)映射到键码。我选择了 F14、F15 和 F16 功能键,但在/usr/include/linux/input-event-codes.h 中定义了可用键码列表;要使用该文件中的名称#defined 作为 hwdb 键码,只需将它们转换为小写并删除key_
前缀。
The default (pc+us) xkb keyboard layout on my computer maps F14
, F15
, and F16
to the XF86Launch5
, XF86Launch6
, and XF86Launch7
keysyms, respectively. If you open xev
now and press the pedals, you should see those keysyms emitted. Using those keysyms, each pedal switch can be mapped as a hotkey in your desktop or window manager.
在我的电脑上的默认(PC +美国)XKB键盘布局映射F14
,F15
以及F16
到XF86Launch5
,XF86Launch6
和XF86Launch7
分别的keysyms。如果您xev
现在打开并踩下踏板,您应该会看到这些键符发出。使用这些键符,每个踏板开关都可以映射为桌面或窗口管理器中的热键。
You can also remap the XF86*
keysyms to other keys using something like xmodmap. For more details, including getting the keys to be mappable in vim, more links to documentation, pointers toward reading the HID device directly if you want, and a solution for Mac OS X, see my weblog post
您还可以XF86*
使用 xmodmap 之类的东西将键符重新映射到其他键。有关更多详细信息,包括在 vim 中获取可映射的键、更多文档链接、指向直接读取 HID 设备的指针(如果需要)以及适用于 Mac OS X 的解决方案,请参阅我的博客文章
Footcontroller
脚踏控制器
One last thing I will mention is a python program called footcontrollerthat reads events from the evdev device (/dev/input/eventX) and can be configured to issue key strokes (via xdotool) or run scripts in response to stepping on the pedals. It can be used in lieu of xmodmap to get your pedal to send any key stroke you want.
我要提到的最后一件事是一个名为footcontroller的python 程序,它从evdev 设备(/dev/input/eventX)读取事件,并且可以配置为发出击键(通过xdotool)或运行脚本以响应踩下踏板。它可以用来代替 xmodmap 来让你的踏板发送你想要的任何击键。