I use an external keyboard with my laptop and a script to change the keyboard layout depending on whether the keyboard is plugged in. This works fine, but I find it shouldn't be necessary to run the script manually: it should run by itself whenever the external keyboard is plugged in or out. I've tried to set this up with a udev rule, but for some reason it doesn't work. My udev rule is:
KERNEL=="3-1.1", SUBSYSTEM=="usb", RUN+="/root/bin/udev.sh"
I have confirmed that this script runs once whenever I plug the keyboard in or out. The contents of the script are:
#!/usr/bin/bash
su - toothrot -c "/usr/local/bin/loadkb.sh" &
And the contents of /usr/local/bin/loadkb.sh
are:
#!/bin/bash
export DISPLAY=:0
export XAUTHORITY=/home/toothrot/.Xauthority
export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
killall xcape
if lsusb | grep HHKB
then {
setxkbmap us,gr \
-variant carpalx-plus-intl,daedalus \
-option grp:win_space_toggle \
-option lv3:rwin_switch \
-option altwin:meta_alt \
-option shift:breaks_caps \
-option lv5:lsgt_switch_lock
xmodmap ~/.xmodmap/hh
xcape -e 'Shift_L=F13;Shift_R=F14;Super_L=Prior;Hyper_L=Prior'
}
else {
setxkbmap us,gr \
-variant carpalx-plus-intl,daedalus \
-option grp:win_space_toggle \
-option lv3:ralt_switch \
-option altwin:meta_alt \
-option shift:breaks_caps -option lv5:lsgt_switch_lock
xmodmap ~/.xmodmap/apple
xcape -e 'Control_L=Escape;Shift_L=F14;Shift_R=F13;Alt_L=Escape;Alt_R=Escape'
}
fi
I have confirmed that running /root/bin/udev.sh
as root works
for my toothrot X session. Only when run by a udev rule, the
script doesn't work. What am I missing?
Or is there a way to achieve the result I want without using a udev rule, maybe from within X?
echo HHKB >/tmp/kbdlog
at the top of thethen
block, andecho other >/tmp/kbdlog
at the top of theelse
, to ensure the correct path is taken/tmp/kbdlog
.stdout
andstderr
of each command to a similar log? That is,command >/tmp/kbdlog 2>&1
for each command? See if any errors pop up there?