In this video I talk about Dmenu and its usage. I also walk you thru the creation of a simple shell script for keyboard layout selection. The full modified script is in the bottom of the description, using test for sorting out “cancel”
Full script:
#!/bin/sh
# Tiny script for selecting keyboard layout while in X
rtrim (){
printf '%s\n' "${1%%$2}"
}
# declare delimiter
delimiter=" -"
# declare languages
languages="se - Swedish\nus - English\nCancel"
# Sending languages to Dmenu
selected=$(echo "$languages" | dmenu)
if test "$selected" != "Cancel" ;then
# Trimming
trimmed="$(rtrim $selected $delimiter)"
setxkbmap $trimmed
fi