Ubuntu – How to change the keyboard layout while using xmonad

11.04keyboardlayoutxmonad

So I have an IBM Thinkpad X31, running natty and xmonad as the window manager. The keyboard is Italian. I wish to use an American English keyboard mapping, all the time. How do I do this?

Best Answer

Here is one way to connect XMonad and multiple keyboard layouts.

Step 1. Create a script that will switch between your layouts. For the sake of example, let's say we will support English (US), Italian and French.

/home/you/bin/layout_switch.sh:

#!/bin/zsh
# LICENSE: PUBLIC DOMAIN
# switch between my layouts

# If an explicit layout is provided as an argument, use it. Otherwise, select the next layout from
# the set [us, it, fr].
if [[ -n "$1" ]]; then
    setxkbmap $1
else
    layout=$(setxkbmap -query | awk 'END{print $2}')
    case $layout in
        us)
                setxkbmap it
            ;;
        it)
                setxkbmap fr
            ;;
        *)
                setxkbmap us
            ;;
    esac
fi

Test this script - run it and see if the keyboard layout cycles between the layouts. If it does, proceed to the next step.

Step 2. Customize XMonad settings to support custom key binding that will switch the layout.

In your home directory, create a directory named ".xmonad" (if it does not exist).

/home/you/.xmonad/xmonad.hs:

import XMonad
import XMonad.Hooks.DynamicLog
import XMonad.Hooks.ManageDocks
import XMonad.Util.Run(spawnPipe)
import XMonad.Util.EZConfig(additionalKeys)
import System.IO

main = do
  xmonad $ defaultConfig
     {
        terminal = "gnome-terminal"
     } `additionalKeys`
     [ (( mod1Mask             , xK_Escape), spawn "/home/you/bin/layout_switch.sh")
     ]

Now, restart XMonad by pressing Mod+q. Your layout switcher should be fully functional.

Reference: http://zuttobenkyou.wordpress.com/tag/setxkbmap/