inputplug
, suggested by etherfish five years ago, is an excellent answer, but the answer was missing a complete example. So here is what I use in my .xinitrc
to run setxkbmap
exactly once in the beginning, and another time every time a new keyboard is plugged in:
{ echo "XIDeviceEnabled XISlaveKeyboard"; inputplug -d -c /bin/echo; } |
while read event
do
case $event in
XIDeviceEnabled*XISlaveKeyboard*)
setxkbmap -option grp:switch,grp:shift_toggle,grp_led:scroll us,il ,lyx
setxkbmap -option caps:none
;;
esac
done &
The inputplug -d -c /bin/echo
echos a message like
XIDeviceEnabled 11 XISlaveKeyboard Logitech K270
every time a keyboard is plugged in, and the while
loop finds such messages and causes the relevant setxkbmap
commands to run (in my example, I set a Hebrew keyboard mapping, and remove the Caps Lock feature which I hate). The extra echo "XIDeviceEnabled XISlaveKeyboard"
in the beginning causes the loop to also find a "new keyboard" exactly once in the beginning. inputplug
also has a "-0" option to print existing keyboards on startup, but this will usually print multiple keyboards and cause the setxkbmap
to be needlessly run multiple times on startup.