Python evdev equivalent for OSX

这一生的挚爱 提交于 2019-12-01 17:54:06

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.

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()

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

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.

I guess there is no evdev port for mac os because last one depends on linux kernel. If you want to implement some business logic on a HID in mac os you should to use as in the comments suggested some high level abstraction. But if you want evdev on low level, I think easy way to do so using the Docker. I have no experience with HID devices on mac os, but I resolved the same problem with the other driver.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!