【问题标题】:Python evdev equivalent for OSX等效于 OSX 的 Python evdev
【发布时间】:2017-04-25 12:56:48
【问题描述】:

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

如果您有 Python 经验并已将其配置为 HID 设备输入,请在您的回复中说明这一点。

【问题讨论】:

标签: python macos barcode-scanner hid evdev


【解决方案1】:

我使用cython-hidapi 进行了一个简单的测试(可安装为pip install hidapi - 请注意,这与 cmets 中链接的不同,但在功能上似乎相似)。我还从 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 设备,所以你得到哪个似乎有点随机,但对于条形码扫描仪来说,它应该非常简单.

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2019-12-11
      • 2014-04-30
      • 1970-01-01
      • 2011-02-13
      • 1970-01-01
      • 2012-06-28
      • 2015-11-17
      • 1970-01-01
      • 2015-03-08
      相关资源
      最近更新 更多