Python evdev等效于OSX

时间:2016-12-10 00:03:02

标签: python macos barcode-scanner hid evdev

我编写了一个python脚本,用于轮询evdev以获取HID条形码扫描器(模拟键盘):该脚本在Linux平台(Ubuntu)上运行良好。是否存在与evdev等效的OS X Python,它允许对现有python脚本进行轻微移植?

如果您有Python经验并已为HID设备输入进行了配置,请在回复中注明。

2 个答案:

答案 0 :(得分:5)

我使用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设备。鼠标和其他东西一样,所以它似乎有点随机你得到的,但对于条形码扫描仪,它应该是非常直接的。

答案 1 :(得分:2)

我猜mac os没有evdev端口,因为最后一个端口依赖于linux内核。如果你想在mac os中的HID上实现一些业务逻辑,你应该像在评论中那样使用一些高级抽象。但如果你想让evdev处于低水平,我认为这样做很简单using the Docker。我没有在mac os上使用HID设备的经验,但我解决了与其他驱动程序相同的问题。