Monday, September 17, 2012

A script to switch the current xkb layout

I recently spent some time setting up a keyboard layout switcher for my Xfce installation. In this post I will share a summary of the main available options I found during the research I did, and the solution I ended up using based on my particular needs.

Requirements

I have an Arch x86_64 + Xfce installed as a guest VM in a Virtualbox on a Windows 7 host. I constantly write documents in Spanish and English on the Linux VM, so I wanted to have a keyboard shortcut to change from 'us' to 'latam' layouts in Xfce.

Very occasionally I also write content in Japanese but, as that content is usually written for web pages or chat conversations, and I very rarely use web browsers or IM apps from the Linux VM, I use the input manager that comes with Windows for this. In other words, I decided not to use an Input Method on my Xfce installation.

Main options

X server configuration file
Manually set the value on the X server configuration file (/etc/X11/xorg.conf) to the desired keyboard layout, as shown in the below example:

...
Option “XkbLayout” “latam”
...

WM / DM integrated app
I am putting here just the apps I tested because they have small print, and are no KDE dependant. These are personal requirements I set up to all the applications I use.
  1. xfce4-xkb-plugin is mentioned in Xfce's FAQ. It is a Xfce-panel plugin that adds an icon to the tray icon area, and allows you to see and change the currently keyboard layout.
    I installed the plugin, but was never able to make it work (it crashed the first time I tried to change the layout, and didn't show up again afterwards).
  2. fbxkb is recommended in Xfce's site. It also shows an icon to the tray icon area, and allows you to see and change the currently keyboard layout. Up to four configurations are allowed (due to X keyboard extension protocol limitations).
    I used it for a while and detected some issues. I find LibreOffice and Lyx sometimes not grabbing the layout change properly (they ignored 'ñ', and á, é, í, ó, ú were displayed as ´a, ´e, ´i, ´o, ´u, respectively). Also, the layout change never worked with Tilda (UTF numeric codes were displayed instead of accents and tildes).
  3. xxkb is a highly configurable layout switcher. It can be used via a window on a tray icon area, or via a small icon on the border of every window showing the current per app layout.
    I wasn't able to use this app because I didn't want to spend much time learning about all the required configuration options (it requires you to specify even the geometry of the main window at the X level). Further, some of the running modes (as the window border one) are not guaranteed to work on all window managers.
setxkbmap
It allows to change the keyboard layout from the command line. It also allows to query the X keyboard extension to get information about the current configuration. After changing the current layout using this command, I tested several desktop and command applications and found no issues.

Selected solution

I ignored the X server file option as I did not want to edit any configuration file (and restart the X server) every time I needed to change the keyboard layout. I also discarded the only WM/DM app I could setup to work (fbxkb) because of its issues didn't let me work fluently.

I ended up writing a very simple script to toggle the current xkb layout between 'us' and 'latam' using setxkbmap, and set it up as a custom keyboard shortcut using Xfce's settings editor.

#!/bin/bash
#

if `setxkbmap -query | grep us > /dev/null`
then
    setxkbmap latam
else
    setxkbmap us
fi
The script was named toggle-xkb and then added to the PATH env variable

No comments:

Post a Comment