使用Python听USB键盘
设置:一个简约的 Linux(华硕路由器上的 OpenWRT),一个 USB 键盘(假设我知道设备名称,如 /dev/hiddev0)
目标:编写一个 python(或 shell,在这种情况下我可以将它用作代理) python)脚本将侦听该设备并根据用户输入执行一些操作
作为开始 - 足以将用户输入的字符回显给给定名称的文本文件。
PyUSB 看起来真的很混乱。 我对 PySerial 没问题,尽管我不确定它是否适合任务 - 当尝试打开设备时,它显示“serial.serialutil.SerialException:无法配置端口:(22,'无效参数')”
UPD:好吧,简单的答案是“cat /dev/input/event1”,
但是输出确实很神秘 - 有任何关于解释它的提示(作为字符 int 代码)吗?
UPD UPD:hexdump /dev/input/event1 好多了! 它为每次按键提供 6 个 9 元组(我想,3 个按键按下,3 个按键按下) 解密它可能并不那么困难
,但仍然高度赞赏更文明的方式
http://svn.navi.cx/misc/trunk/python/evdev/evdev.py 看起来很有趣......
The setup: a minimalistic Linux (OpenWRT on ASUS router), a USB keyboard (assume I know the device name like /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
As a beginning - quite enough to echo user-entered characters to text file of given name.
PyUSB looks really confusing.
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')"
UPD: well, OK, the trivial answer was "cat /dev/input/event1"
But the output is really cryptic - any hint on interpreting it (as character int codes)?
UPD UPD: hexdump /dev/input/event1 is much better!
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
But more civil way is still highly appreciated
http://svn.navi.cx/misc/trunk/python/evdev/evdev.py looks interesting...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
http://svn.navi.cx/misc/trunk/python/evdev /evdev.py 是真正的答案 - 输出非常详细,并且有现成的
.poll()
方法返回事件。我遇到的唯一问题 - readMetadata() 失败,错误 22。然后我评论了它的调用,它
实际上就像一个魅力,我从 http://autokey.googlecode.com/svn/trunk/src/lib/evdev.py
添加:发现实际eventN,
并仔细阅读它
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.the only problem I encountered - readMetadata() failed with Error 22. Then I commented its call out, and it worked like a charm
actually, I downloaded some more recent version from http://autokey.googlecode.com/svn/trunk/src/lib/evdev.py
Add: to discover the actual eventN,
and carefully read through it
对于后来发现这个问题的人(就像我身上发生的那样):Georgi Valkov 为 evdev 提供了一个很棒的库:
https://github.com/gvalkov/python-evdev
http://python-evdev.readthedocs.org/en/latest/index.html
For anyone who discovers this question later (as happened to me): there is a wonderful library by Georgi Valkov for evdev:
https://github.com/gvalkov/python-evdev
http://python-evdev.readthedocs.org/en/latest/index.html