Skip to content

Instantly share code, notes, and snippets.

@abrahammurciano
Last active June 21, 2017 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abrahammurciano/312a75430d9fc99c5786c974d1439628 to your computer and use it in GitHub Desktop.
Save abrahammurciano/312a75430d9fc99c5786c974d1439628 to your computer and use it in GitHub Desktop.
Automatically switches keyboard layout when USB keyboard is attached. Flowchart of process available at https://creately.com/diagram/j472uvyq1/CW4hZYa3gGMGo8tYR8Ks5bPtQ%3D. Currently set to automatically use Spanish (es) and English UK (gb) layout. You can customize it. All layouts can be found in /usr/share/X11/xkb/symbols
#!/bin/bash
kbd1=es #default keyboard layout
kbd2=gb #USB keyboard layout
while true
do
layout="$(setxkbmap -query | grep -a layout | cut -c13-14)"
for dev in /sys/bus/usb/devices/*-*:*
do
#If kbd is in
if [[ "$(cat $dev/bInterfaceClass)" == "03" && "$(cat $dev/bInterfaceProtocol)" == "01" ]]
then
#If kbs is spanish
if [[ $layout == $kbd1 ]]
then
setxkbmap $kbd2
layout=$kbd2
fi
#Break FOR loop and wait 5 for next iteration of WHILE loop
break
fi
if [[ $layout == $kbd2 ]]
then
setxkbmap $kbd1
layout=$kbd1
fi
done
sleep 5s
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment