简体   繁体   English

用Python听USB键盘

[英]Listen to USB keyboard with Python

The setup: a minimalistic Linux (OpenWRT on ASUS router), a USB keyboard (assume I know the device name like /dev/hiddev0) 设置:简约Linux(华硕路由器上的OpenWRT),USB键盘(假设我知道设备名称如/ dev / hiddev0)

A goal: write a python (or shell, in this case I can use it like a proxy for python) script that will listen to this device and perform some actions based on the user input 一个目标:编写一个python(或shell,在这种情况下,我可以使用它像python的代理)脚本,它将监听此设备并根据用户输入执行一些操作

As a beginning - quite enough to echo user-entered characters to text file of given name. 作为开头 - 足以将用户输入的字符回显给给定名称的文本文件。

PyUSB looks really confusing. PyUSB看起来真的很混乱。 I'm ok with PySerial, though I'm not sure if it suits the task - when trying to open the device it says "serial.serialutil.SerialException: Could not configure port: (22, 'Invalid argument')" 我对PySerial没问题,虽然我不确定它是否适合任务 - 当试图打开设备时它说“serial.serialutil.SerialException:无法配置端口:(22,'无效参数')”

UPD: well, OK, the trivial answer was "cat /dev/input/event1" UPD:嗯,好吧,琐碎的回答是“cat / dev / input / event1”

But the output is really cryptic - any hint on interpreting it (as character int codes)? 但输出真的很神秘 - 任何解释它的提示(作为字符int代码)?

UPD UPD : hexdump /dev/input/event1 is much better! UPD UPD :hexdump / dev / input / event1要好得多! it gives 6 9-tuples for each key press (I suppose, 3 for key down, 3 for key up) It would probably be not so difficult to decrypt it 它为每个按键提供6个9元组(我想,3个用于按键,3个用于按键)解密它可能不是那么困难

But more civil way is still highly appreciated 但更多的民间方式仍然受到高度赞赏

http://svn.navi.cx/misc/trunk/python/evdev/evdev.py looks interesting... http://svn.navi.cx/misc/trunk/python/evdev/evdev.py看起来很有意思......

http://svn.navi.cx/misc/trunk/python/evdev/evdev.py is the real answer - the output is greatly detailed, and there's ready .poll() method that returns events. http://svn.navi.cx/misc/trunk/python/evdev/evdev.py是真正的答案 - 输出非常详细,并且已经准备好了返回事件的.poll()方法。

the only problem I encountered - readMetadata() failed with Error 22. Then I commented its call out, and it worked like a charm 我遇到的唯一问题 - readMetadata()失败,出现错误22.然后我评论了它的调用,它就像一个魅力

actually, I downloaded some more recent version from http://autokey.googlecode.com/svn/trunk/src/lib/evdev.py 实际上,我从http://autokey.googlecode.com/svn/trunk/src/lib/evdev.py下载了一些更新的版本。

Add: to discover the actual eventN, 添加:发现实际的事件N,

cat /proc/bus/input/devices

and carefully read through it 并仔细阅读它

For anyone who discovers this question later (as happened to me): there is a wonderful library by Georgi Valkov for evdev: 对于后来发现这个问题的人(就像发生在我身上一样):Georgi Valkov为evdev提供了一个很棒的图书馆:

https://github.com/gvalkov/python-evdev https://github.com/gvalkov/python-evdev

http://python-evdev.readthedocs.org/en/latest/index.html http://python-evdev.readthedocs.org/en/latest/index.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM