HowTo: have/display multiple keyboard layouts on Unix-like

I would like to share with you my latest discovery: multi-layout keyboards on Unix-like systems.
First step is to add/edit the following file: /usr/local/etc/X11/xorg.conf.d/kbd-layout-multi.conf and add your wanted layouts. In this howto I will add 4 layouts: us, ro, fr, de (american-english, romanian, french and german):

Code:
Section    "InputClass"
    Identifier    "All Keyboards"
    MatchIsKeyboard    "yes"
    Option        "XkbLayout" "us,ro,fr,de"
    Option      "XkbVariant" ",std,oss_nodeadkeys,"
    Option      "XkbOptions" "compose:paus,lv3:ralt_switch,grp:ctrl_shift_toggle_bidir"
EndSection

Let's explain a little what's in this file:
i) XkbLayout - obviously it's for the languages we wish;
ii) XkbVariant - it's for keyboards variants, on my machine I wanted standard keyboard for romanian and no dead keys for french & german(",std,oss_nodeadkeys,").
iii) XkbOptions - here I've mapped the Pause key (compose: paus) so I can combine multiple keys to obtain difficult chars (eg. Pause +c+,=ç,).
_ lv3:ralt_switch (AltGr) - it's also for accessing different chars (eg. ø, µ, ł, ĸ, ŋ, đ, ð, ſ, æ, etc.).
_ grp:ctrl_shift_toggle_bidir - Ctrl+Shift which are dead keys in i3WM are used so I can switch between layouts in both direction (Left Ctrl+Shift is backward & Right Ctrl+Shift is forward)

Second step it's to use a keyboard layout indicator and switcher. Even if it wasn't my first choice x11/xkb-switch it's a good tool to do this. Since I'm using a WM (i3 and i3blocks as status), all I have to do it's to create a simple script which display the layouts.

Code:
#!/bin/sh

layout=$(xkb-switch -p | cut -c 1-2)


case $layout in
   us) echo "US"
       ;;
   ro) echo "RO"
       ;;
   fr) echo "FR"
       ;;
   de) echo "DE"
      ;;
esac

The result:
us_layout.pngro_layout.pngfr_layout.pngde_layout.png
 
I use a similar approach except x11/xkb-switch: I wrote my own program.
I disabled Caps-Lock and mapped the following combinations to scripts which switch layouts the following way:
CapsLock: US English
Shift-CapsLock: Armenian
Ctrl-CapsLock: Russian

Also I created a graphical indicator using x11/dzen2. It allows to switch layouts with mouse clicks as well:
Code:
#!/bin/bash
# Position and size
XPOS='1286'
YPOS='0'
HEIGHT='18'
WIDTH='32'

# Flag icons
US_ICO='~/icons/us.xpm'
RU_ICO='~/icons/ru.xpm'
HY_ICO='~/icons/hy.xpm'
ZZ_ICO='~/icons/unknown.xpm'

# This script path to be relaunch with mouse clicks
XKB='~/scripts/xkb'
# Layout switcher program:
SWITCHER='~/scripts/xkb3'
# Local dzen copy to track its PID:
DZEN='local_dzen'
# Mouse clicks actions
ACT="button1=exec:$XKB 0;button3=exec:$XKB 1;button2=exec:$XKB 2"

PID=`pidof $DZEN`

if [ "x${PID}" != "x" ]
then
    PAR_PID=`ps -o ppid= -p $PID`
        kill $PAR_PID
        kill $PID
fi

    case "`$SWITCHER $1 $2`" in
    "0")
        XKB_ICO=$US_ICO
        ;;
    "1")
        XKB_ICO=$RU_ICO
        ;;
    "2")
        XKB_ICO=$HY_ICO
        ;;
    *)
        XKB_ICO=$ZZ_ICO
        ;;
    esac

echo ^i\("$XKB_ICO"\) | $DZEN -ta c -tw $WIDTH -h $HEIGHT -bg midnightblue -x $XPOS -y $YPOS -fn $FONT -e "$ACT" -p
The script above is far from being perfect, but it works reliably for many years in 3 computers, so I'm too lazy to improve it.
 
Back
Top