Python evdev equivalent for OSX

后端 未结 2 692
死守一世寂寞
死守一世寂寞 2021-01-18 12:40

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

2条回答
  •  再見小時候
    2021-01-18 12:54

    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 
        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.

提交回复
热议问题