Python evdev等效于OSX

gat*_*ack 6 python macos hid barcode-scanner evdev

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

如果您具有Python经验并已针对HID设备输入进行了配置,请在响应中指明.

Pet*_*son 5

我使用cython-hidapi进行了一个简单的测试(可安装为pip install hidapi- 注意这与注释中链接的那个不同,但在功能上似乎相似).我也hidapi-devel从macports 安装但我不确定这是否必要,因为它在停用端口后继续工作.

通过修改示例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()
Run Code Online (Sandbox Code Playgroud)

$ 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
Run Code Online (Sandbox Code Playgroud)

我正在使用的特定设备似乎被列举为键盘和鼠标等多个HID设备,所以它似乎有点随机你得到的,但对于条形码扫描仪,它应该是非常直接的.