Change keyboard maps

To be able to switch between different keyboard languages/layouts and to display the current layout, you can use external applications or add widgets.

Awesome WM solutions
These widgets indicate the current layout and allow switching by clicking the widget, using keyboard shortcuts or both.

setxkbmap method
This widget displays the layout used by the keyboard. If clicked, it switchs between the list of user defined layouts. You have to set the current index to your default layout in order to be consistent with your system configuration.

'setxkbmap' is used to set the layout.

rc.lua code
Paste the following code into your rc.lua somewhere before the wibox definition. Custom arguments can be passed in the kbdcfg.layout array. Replace Foo and Bar with custom names that will appear in the widget.

-- Keyboard map indicator and changer kbdcfg = {} kbdcfg.cmd = "setxkbmap" kbdcfg.layout = { { "us", "", "Foo" }, { "us", "dvorak" , "Bar" } } kbdcfg.current = 1 -- us is our default layout kbdcfg.widget = wibox.widget.textbox kbdcfg.widget:set_text(" " .. kbdcfg.layout[kbdcfg.current][3] .. " ") kbdcfg.switch = function kbdcfg.current = kbdcfg.current % #(kbdcfg.layout) + 1 local t = kbdcfg.layout[kbdcfg.current] kbdcfg.widget:set_text(" " .. t[3] .. " ") os.execute( kbdcfg.cmd .. " " .. t[1] .. " " .. t[2] ) end -- Mouse bindings kbdcfg.widget:buttons( awful.util.table.join(awful.button({ }, 1, function  kbdcfg.switch end)) )

Then add kbdcfg.widget to your wibox and restart Awesome.

-- Add widget to your layout right_layout:add(kbdcfg.widget)

If you want your default keyboard layout to be something else than the standard US, you would need to click on the item once after every restart. To fix this, add the following to your ~.xinitrc :

setxkbmap de
 * 1) Set the keyboard layout once at startup.
 * 2) (Replace "de" with the layout of your choice.)

Legacy code for 3.4:

-- Keyboard map indicator and changer kbdcfg = {} kbdcfg.cmd = "setxkbmap" kbdcfg.layout = { "us", "fr", "dvorak" } kbdcfg.current = 1 -- us is our default layout kbdcfg.widget = widget({ type = "textbox", align = "right" }) kbdcfg.widget.text = " " .. kbdcfg.layout[kbdcfg.current] .. " "    kbdcfg.switch = function kbdcfg.current = kbdcfg.current % #(kbdcfg.layout) + 1 local t = " " .. kbdcfg.layout[kbdcfg.current] .. " "       kbdcfg.widget.text = t        os.execute( kbdcfg.cmd .. t ) end -- Mouse bindings kbdcfg.widget:buttons(awful.util.table.join( awful.button({ }, 1, function kbdcfg.switch end) ))

Enabling accelerator keys
Accelerator keys (e.g. CTRL-C in Firefox) will not work in layouts lacking Latin letters. A simple way to fix this is to always pass the "us" layout to setxkbmap after the primary layout.

kbdcfg.current = kbdcfg.current % #(kbdcfg.layout) + 1 local layout = kbdcfg.layout[kbdcfg.current] kbdcfg.widget.text = " " .. layout .. " "   os.execute( kbdcfg.cmd .. " " .. layout .. ",us" )

This seems inefficient but works fine. Alternatives welcome.

Keyboard bindings
Example global keybindings to switch layouts:

-- Alt + Right Shift switches the current keyboard layout awful.key({ "Mod1" }, "Shift_R", function kbdcfg.switch end),

setxkbmap method
Configure your X server with ~/.xinitrc or similar by adding the following line: setxkbmap us,ru

KDE 4 method
To remember the last layout used on every window, the KDE 4 keyboard service can be started manually. To do this you will need some KDE packages installed (in Ubuntu you'll need at least kdelibs-bin and kde-workspace-bin) and the qdbus utility. Add the following to your ~/.xinitrc file:

(pidof kded4 || kded4) && qdbus org.kde.kded /kded loadModule keyboard &

Once the service is running, you'll see a neat keyboard layout indicator (with country flags, if you have them installed). You can right click it and choose Configure (alternatively, you can run 'kcmshell4 keyboard' from the command line), and set the Switching Policy to your preference.