Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python evdev equivalent for OSX

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?

If you have Python experience and have configured it for a HID device input, please indicate this in your response.

like image 549
gatorback Avatar asked Dec 10 '16 00:12

gatorback


2 Answers

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.

like image 67
Peter Gibson Avatar answered Nov 03 '22 10:11

Peter Gibson


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.

like image 2
fedorshishi Avatar answered Nov 03 '22 10:11

fedorshishi