X11 keyboard layout switch

Please note that this blog has been moved.

Now it has its own domain: mynixworld.info 🙂

If you want to read the latest version of this article (recommended) please click here and I open the page for you.

If you are looking for a versatile keyboard layout application (in Xfce) then you should take a look at xfce-extra/xfce4-xkb-plugin.

If you only need a dual (also multiple layout are possible) keyboard switch which will work on almost any X11 desktop and do not want to install a special plugin (because you love to have a lightweight installation) then you can try the following solution:

  • create a bash script (eg: switch_kbs) like the one bellow (don’t forget the chmod +x switch_kbs)
  • create a keyboard shortcut (eg: Alt+Shift) that will point to your switch_kbs script
  • anytime you want to switch between the keyboard layouts just press the keyboard combination as defined above
kb=$(setxkbmap -query|awk '/layout/ {print $2}')
case $kb in
se) kb="us"
us) kb="se"
setxkbmap -layout $kb
/usr/bin/notify-send -i "/usr/share/icons/Tango/48x48/apps/accessories-character-map.png" "Keyboard layout" "Layout : $kb"

Note: setxkbmap should come with X11 so it should be already available on any Linux desktop (I guess).

If you need more than two keyboard layouts then you should change little bit the “case” statement above. In my example the “case” is doing the following:

  • when is “se” then move to the next one : “us”
  • when is “us” then move to the next one : “se”
  • you can continue like this with many other layouts; what is important is that the circle to close somewhere, I mean the “case” to switch between all the layouts in circle.

Edit: what happen if you want to see the keyboard layout (how the keys are arranged on virtual keyboard)? I have a solution to this too:

  • we create a shell script that will display a picture on the screen containing the keyboard’s layout; for this we’ll need x11-misc/gtkdialog
  • you can name that script like kblayout-<xx>, where xx is the language code (it’s just a name, really)

export script='<window title="Svensk tangentbord layout"><pixmap><input file>/home/eugen/Pictures/Keyboard_Layout_Swedish.png</input><height>$
gtkdialog --program=script

Later, you can create a keyboard shortcut that will launch this script whenever you want to see the keyboard layout (eg: Alt+Shift+S):

About Eugen Mihailescu

Always looking to learn more about *nix world, about the fundamental concepts of arithmetic, algebra and geometry. I am also passionate about programming, database and systems administration.
This entry was posted in linux, X11 and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s