Is there any daemon/tool which will trigger some signal/event when USB keyboard is plugged and unplugged from pc? I need to know in my program when USB keyboard is plugged and unplugged. Any ideas on how to do this?
3 Answers
udev (Linux device manager) is the one that polls hardware. When it detects some changes about devices, it executes the rule associated.
You should add a rule to udev, in order to inform your program about insertion of removal of USB keyboard. You can find documentation about udev rules here, or Look at files in /etc/udev/rules.d/
or /lib/udev/rules.d/
to find many examples.

- 25,562
- 20
- 98
- 150
-
Such simple thing as watching for hardware changes does not have to be that hard, and does not have to require root privileges. See my answer below how to do this using DBus and HAL. – abbot Dec 06 '10 at 12:51
udevadm monitor
(the udev administration binary) or udev_monitor
(in libudev).
Alternately, if you're running in X11 with input hotplugging, you can listen for the XI extension event DevicePresenceNotify
.

- 198,619
- 38
- 280
- 391
If HAL daemon is running (which is true for most modern linux desktops), you can listen to its DBus Signals. Here is an example how to detect if a USB mouse is plugged in (I don't have a USB keyboard at hand):
import gobject
import dbus
from dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
# enumerate all present mice:
manager = dbus.Interface(bus.get_object("org.freedesktop.Hal",
"/org/freedesktop/Hal/Manager"),
"org.freedesktop.Hal.Manager")
mice = set(manager.FindDeviceByCapability('input.mouse'))
def device_added(sender):
dev = dbus.Interface(bus.get_object("org.freedesktop.Hal", sender),
"org.freedesktop.Hal.Device")
try:
caps = dev.GetProperty('info.capabilities')
if 'input.mouse' in caps:
print "mouse plugged in"
mice.add(sender)
except dbus.DBusException:
pass
def device_removed(sender):
if sender in mice:
print "mouse unplugged"
mice.remove(sender)
bus.add_signal_receiver(device_added, signal_name="DeviceAdded")
bus.add_signal_receiver(device_removed, signal_name="DeviceRemoved")
loop = gobject.MainLoop()
loop.run()

- 27,408
- 6
- 54
- 57
-
2
-
Actually HAL is deprecated and no longer maintained in favor of udev; only outdated applications use it. I agree, however, that your solution is simpler than mine and doesn't require root access. Don't know if it's possible to do something like that with non-deprecated tools, but HAL should be avoided. – peoro Dec 06 '10 at 12:59
-
HAL may be deprecated, however its replacement DeviceKit is very poor in some aspects. Quoting X.Org Wiki, "Neither DeviceKit, nor the udisks/upower/etc. replacements provide any of this functionality for input devices, and the DeviceKit authors have indicated that they do not plan to provide such functionality, suggesting direct use of the OS interfaces such as libudev instead." I would continue to use HAL for these functions, since it is still included in most distributions. – abbot Dec 06 '10 at 18:39
-
1[Fedora HAL Removal](http://fedoraproject.org/wiki/Features/HalRemoval), [Debian HALectomy](http://wiki.debian.org/HALRemoval), [Ubuntu Halsectomy](https://wiki.ubuntu.com/Halsectomy), etc. HAL is either already removed from the default install or soon to be. – ephemient Dec 06 '10 at 19:37
-
Yep, I'm aware of that fact. But still: a) commercially supported linux distros continue to use HAL and b) there are no complete user-level replacements. No really, root privileges just to get notifications about plugging/unplugging a usb keyboard or mouse? – abbot Dec 06 '10 at 22:55
-
(lib)udev can get notifications without root permissions. And, if OP just cares about X11 input devices, it's better to watch for XI events than the underlying hardware anyhow. – ephemient Dec 08 '10 at 18:48
-
-
@Pithikos, the original response to question predates wide adoption of udisks or even wide availability of non-HAL LTS Linux releases. – abbot Nov 16 '14 at 22:05