[英]Python evdev equivalent for OSX
我编写了一个python脚本,用于轮询evdev以获取HID条形码扫描器(模拟键盘):该脚本在Linux平台(Ubuntu)上运行良好。 是否存在与evdev等效的OS X Python,它允许对现有python脚本进行轻微移植?
如果您具有Python经验并已针对HID设备输入进行了配置,请在响应中指明。
我使用cython-hidapi进行了一个简单的测试(可安装为pip install hidapi
- 注意这与注释中链接的不同,但在功能上似乎相似)。 我也从macports安装了hidapi-devel
,但我不确定这是否必要,因为它在停用端口后继续工作。
通过修改示例try.py来使用Microsoft USB无线键盘/鼠标设备的VID / PID,如下所示
from __future__ import print_function
import hid
import time
print("Opening the device")
h = hid.device()
h.open(1118, 2048) # A Microsoft wireless combo keyboard & mouse
print("Manufacturer: %s" % h.get_manufacturer_string())
print("Product: %s" % h.get_product_string())
print("Serial No: %s" % h.get_serial_number_string())
try:
while True:
d = h.read(64)
if d:
print('read: "{}"'.format(d))
finally:
print("Closing the device")
h.close()
并运行$ sudo python try.py
我能得到以下输出:
Opening the device
Manufacturer: Microsoft
Product: Microsoft® Nano Transceiver v2.0
Serial No: None
read: "[0, 0, 0, 0, 0, 0, 0, 0]"
read: "[0, 0, 0, 0, 0, 0, 0, 0]"
read: "[0, 0, 0, 0, 0, 0, 0, 0]"
--8<-- snip lots of repeated lines --8<--
read: "[0, 0, 0, 0, 0, 0, 0, 0]"
read: "[0, 0, 0, 0, 0, 0, 0, 0]"
read: "[0, 0, 21, 0, 0, 0, 0, 0]"
read: "[0, 0, 21, 0, 0, 0, 0, 0]"
read: "[0, 0, 21, 0, 0, 0, 0, 0]"
read: "[0, 0, 21, 0, 0, 0, 0, 0]"
read: "[0, 0, 0, 0, 0, 0, 0, 0]"
read: "[0, 0, 4, 0, 0, 0, 0, 0]"
read: "[0, 0, 4, 22, 0, 0, 0, 0]"
read: "[0, 0, 4, 22, 0, 0, 0, 0]"
read: "[0, 0, 4, 22, 0, 0, 0, 0]"
read: "[0, 0, 4, 22, 0, 0, 0, 0]"
read: "[0, 0, 4, 22, 0, 0, 0, 0]"
read: "[0, 0, 4, 0, 0, 0, 0, 0]"
read: "[0, 0, 4, 0, 0, 0, 0, 0]"
read: "[0, 0, 4, 9, 0, 0, 0, 0]"
read: "[0, 0, 4, 9, 0, 0, 0, 0]"
read: "[0, 0, 4, 9, 0, 0, 0, 0]"
read: "[0, 0, 4, 9, 0, 0, 0, 0]"
read: "[0, 0, 4, 9, 7, 0, 0, 0]"
read: "[0, 0, 4, 9, 7, 0, 0, 0]"
read: "[0, 0, 7, 0, 0, 0, 0, 0]"
^CClosing the device
Traceback (most recent call last):
File "try.py", line 17, in <module>
d = h.read(64)
KeyboardInterrupt
我正在使用的特定设备似乎被列举为键盘和鼠标等多个HID设备,所以它似乎有点随机你得到的,但对于条形码扫描仪,它应该是非常直接的。
我猜mac os没有evdev端口,因为最后一个端口依赖于linux内核。 如果你想在mac os中的HID上实现一些业务逻辑,你应该像在评论中那样使用一些高级抽象。 但是如果你想让evdev处于低级别,我认为使用Docker可以轻松实现这一目标 。 我没有在mac os上使用HID设备的经验,但我解决了与其他驱动程序相同的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.