gpt4 book ai didi

OSX 的 Python evdev 等价物

转载 作者:太空狗 更新时间:2023-10-29 22:29:06 24 4
gpt4 key购买 nike

我编写了一个 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 设备等,所以你得到哪个似乎有点随机,但对于条形码扫描仪来说它应该非常简单.

关于OSX 的 Python evdev 等价物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41070706/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com