WalkingThrough
Posts: 3
Joined: Sat Aug 23, 2014 11:47 pm

Using udev for keyboard/mouse insert detect?

Wed Mar 29, 2017 4:18 pm

Hello everyone -

Platform: RPi Model 3. 5V @ 2.5A power supply. Using HDMI-attached display and there is a reliable boot. No problems there.

I've been at this for a while. I'm trying to detect if a keyboard has been plugged into a USB port. So far, so good: There is now a udev rule in /etc/udev/rules.d/10-local.rules that reads as follows:

ACTION=="add",DRIVER=="usb",ATTR{bInterfaceClass}=="03","ATTRS{bInterfaceProtocol}=="01",RUN="/etc/udev/rules.d/doit"

doit is a program that sends a short message to another program (running as user pi). That program turns off the capslock and numlock LEDs. I can, as root, run "doit" with the capslock LED turned on... and the LED goes off just as desired. This works well with a wired keyboard.

As a test... Comment out the udev rule, run udevadm control --reload to reload the rules, plug in the keyboard, turn the LED on by pressing Caps Lock. Unplug the keyboard, wait a moment, then plug it back in. After a moment, the LED comes back on. Fine.

Re-enable the udev rule, reload the rules, turn on the LED, unplug the keyboard, plug it back in. Result: the LED does not turn on. Looks like that works.

Now I try running this with a wireless Logitech keyboard/mouse combination... and there seems to be a problem.

With the Logitech dongle in place and everything set up (rule in place, user-land program waiting) turn on capslock. LED goes on. Unplug the dongle. The keyboard is battery powered... and the LED stays lit.

Plug in the dongle.

udev starts "doit". "doit" sends its message. The receiver gets the message and tries to turn the LED off.

The LED stays on.

The receiver program has some X Windows code in it... and everything there runs without error.

Started udevadm monitor and watched events fly by. It looks like the keyboard sets up before the mouse... and the keyboard ?misses? the message?

In one case a plain ol' keyboard. In another, a wireless keyboard/mouse combination with a dongle. How do I write a udev rule that properly starts "doit" on either a keyboard or a keyboard+mouse event?

Return to “Advanced users”