I have a Ubuntu 20 LTS laptop with a Spanish QWERTY keyboard. For ease of finding backticks and other special characters while coding, I've purchased an additional external USB keyboard.
I'd like to make it so that when the external keyboard is connected, the main one is disabled (so I can place it on top without that pressing the keys resulting in unwanted input), and so that when the external keyboard is disconnected, the main one gets enabled again.
I've come up with a script to run as a daemon and poll to see if the external keyboard is connected.
#!/bin/bash
while true; do
if [ $(xinput -list | grep "SEM USB Keyboard" | wc -l) -gt 0 ]; then
echo "External keyboard found"
xinput --disable "AT Translated Set 2 keyboard"
echo "Main keyboard disabled"
else
echo "External keyboard NOT found"
xinput --enable "AT Translated Set 2 keyboard"
echo "Main keyboard enabled"
fi
echo "Waiting..."
sleep 5
done
Two questions here.
- The script works perfectly when run in the foreground, but when run in the background like so:
setsid kb-disabler-daemon.sh >/dev/null 2>&1 < /dev/null &
Then, it fails to enable the main keyboard again when the external one is disconnected. What am I doing wrong?
- Is there a simpler way to do this? Maybe a utility app or configuration of sorts that will do this?
setsid
). Would you check this similar issue solution (user switch + DISPLAY variable): https://askubuntu.com/a/137278/26246 – user.dz Jul 02 '22 at 10:34wc -l
can be replace by the the-c
option ofgrep
I think – UnderTheHoud Jul 02 '22 at 11:53