larsks
larsks

Reputation: 311606

Interacting with raw HID devices using Python?

I am trying to write some Python code to communicate with a device (a keyboard) via its raw hid interface. The keyboard presents multiple HID devices to the host (keyboard, system keys, mouse, raw hid, etc), and I can only identify the correct device by looking at the "Usage page" and "Usage" attributes.

I looked at the Python hid module (as packaged in python3-hidapi-0.7.99.post20-13.fc31.x86_64 in Fedora 31), but in the return value from hid.enumerate() the values for usage_page and usage are 0 in all cases:

>>> import hid
>>> import pprint
>>> pprint.pprint([(x['path'], x['usage_page']) for x in hid.enumerate() if x['vendor_id'] == 1240 and x['product_id'] == 61138])
[(b'0002:0010:00', 0),
 (b'0002:0010:02', 0),
 (b'0002:0010:03', 0),
 (b'0002:0010:01', 0),
 (b'0002:0010:04', 0),
 (b'0002:0010:05', 0)]

Is there any way to get at the usage_page information from Python? It looks like this is exposed in the device report descriptor available in sysfs via /sys/class/hidraw/hidraw<N>/device/report_descriptor, but I'd like to avoid reinventing the wheel by writing my own enumeration code if possible.

Upvotes: 1

Views: 3577

Answers (1)

Youw
Youw

Reputation: 879

Under the hood, your python application uses hidapi C library, which as of this moment doesn't support getting usage/usage_page-es on Linux.

So, answering your direct question: currently there is no way to get usage/usage_page directly from Python, specifically, using hid library.

There is an active discussion to add such support of-the-box: https://github.com/libusb/hidapi/pull/139

Upvotes: 1

Related Questions