问题描述
我编写了一个Python脚本,该脚本轮询evdev的HID条码扫描器(模拟键盘):该脚本在Linux平台(Ubuntu)上运行良好.是否有适用于evdev的OS X Python等效版本,可允许对现有python脚本进行较小的移植?
I have written a python script that polls evdev for a HID barcode scanner (emulates a keyboard): the script works well on Linux platforms (Ubuntu). Is there an OS X Python equivalent for evdev that would allow minor porting of the existing python script?
如果您具有Python经验并已将其配置为HID设备输入,请在您的回复中注明.
If you have Python experience and have configured it for a HID device input, please indicate this in your response.
推荐答案
我使用 cython进行了简单的测试-hidapi (可作为pip install hidapi
安装-请注意,这与注释中链接的内容不同,但功能似乎相似).我还从macports安装了hidapi-devel
,但是我不确定这样做是否必要,因为在停用端口后它仍然可以工作.
I got a simple test working using cython-hidapi (installable as pip install hidapi
- note this is different to the one linked in the comments but seems to be similar in function). I also had installed hidapi-devel
from macports but I'm not sure that this is necessary as it continues to work after deactivating the port.
通过修改示例 try.py 来使用Microsoft USB无线键盘/鼠标设备的VID/PID如下
By modifying the example try.py to use the VID/PID of a Microsoft USB wireless keyboard/mouse device as follows
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
运行,我可以得到以下输出:
And running with $ sudo python try.py
I was able to get the following output:
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设备.鼠标等等,所以您得到的似乎有点随机,但是对于条形码扫描仪来说,它应该很简单.
The particular device I'm using seems to enumerate as multiple HID devices for the keyboard & mouse amongst other things, so it seems to be a bit random which one you get, but for a barcode scanner it should be pretty straight forward.
这篇关于适用于OSX的Python evdev的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!