Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if USB keyboard is plugged and unplugged

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?

like image 603
deimus Avatar asked Oct 14 '22 20:10

deimus


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

like image 62
peoro Avatar answered Nov 08 '22 12:11

peoro


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.

like image 24
ephemient Avatar answered Nov 08 '22 11:11

ephemient