I had a working shell script to assign a different layout to an external USB keyboard when it is plugged in, and it no longer works. I don't know it is because I switch from sh to zsh.
usbkbd_id="$(xinput -list | grep "USB Keyboard" | awk -F'=' '{print $2}' | grep "keyboard" | cut -c 1-2)"
for ID in $usbkbd_id; do
setxkbmap -device ${ID} -layout "es"
done
I've noticed that when I checked it with command echo "device: ${ID} layout: ${usbkbd_layout}" >> test.txt
in deed it's not looping through each ID, but simply adding "device: "at the beginning of the whole first variable output...
I've gone through similar questions and answers, trying different alternatives I've seen, like ${usbkbd_id[@]}
, with my beginner level scripting skills, but I couldn't solve this problem.
Actually, maybe the real problem at the root is that I always get 3 IDs for my external USB keyboard, and actually assigning the keyboard layout to only one of them is effective, and I can't determine which. So sometimes this command works, sometimes it doesn't (when the correct ID is not the last one I guess).
xinput list --id-only 'actual name of USB Keyboard device'
– rowboat Dec 06 '20 at 13:37xinput -list | grep "USB Keyboard"
. Now `xinput list --id-only 'SEM USB Keyboard' will result in a cleaner command (excluding 2 IDs for 'SEM USB Keyboard Consumer Control' and 1 ID for 'SEM USB Keyboard System Control' which apparently I don't need). – Sadi Dec 06 '20 at 14:02