I have some Python code that interacts with a wireless USB numeric keypad. I'd like to be able to discover the device by name, but shows up as multiple input devices. In /dev/input/by-id
:
# ls -l /dev/input/by-id
total 0
lrwxrwxrwx 1 root root 9 Sep 25 14:30 usb-MOSART_Semi._2.4G_Keyboard_Mouse-event-kbd -> ../event0
lrwxrwxrwx 1 root root 9 Sep 25 14:30 usb-MOSART_Semi._2.4G_Keyboard_Mouse-if01-event-mouse -> ../event1
lrwxrwxrwx 1 root root 9 Sep 25 14:30 usb-MOSART_Semi._2.4G_Keyboard_Mouse-if01-mouse -> ../mouse0
And using the evdev
module:
>>> import evdev
>>> devices = [evdev.InputDevice(dev) for dev in evdev.list_devices()]
>>> for device in devices:
... print(device.fn, device.name)
...
/dev/input/event1 MOSART Semi. 2.4G Keyboard Mouse
/dev/input/event0 MOSART Semi. 2.4G Keyboard Mouse
Obviously the kernel can distinguish the -kbd
device from the -mouse
device, but how do I make that determination in my code? The solution I've come up with for now takes advantage of the fact that the "keyboard" device includes the KEY_KP...
capabilities, so I can do this:
def is_keyboard(device):
return evdev.ecodes.KEY_KP1 in device.capabilities()[evdev.ecodes.EV_KEY]
...but that seems more of a heuristic than a reliable test. Is there a way to detect, using the evdev
module or an alternative, whether or not a given input device is a keyboard or a mouse (or some frankenstein-like combination of the two)?
It seems no way to directly identify if a given input device is a keyboard or a mouse in
evdev
. But you can usesubprocess
as an alternative.Then you can get a list like: