4

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.

  1. 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?

  1. Is there a simpler way to do this? Maybe a utility app or configuration of sorts that will do this?
Mig82
  • 145
  • Possibly due to confined environment (created by 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:34
  • I guess you have to give the absolute path of your script when using setsid. At least it worked like this as I tested. – UnderTheHoud Jul 02 '22 at 11:52
  • Also wc -l can be replace by the the -c option of grep I think – UnderTheHoud Jul 02 '22 at 11:53
  • You also perhaps may want to execute the script at startup without every time typing that command. https://askubuntu.com/questions/814/how-to-run-scripts-on-start-up – UnderTheHoud Jul 02 '22 at 12:01

0 Answers0