This wiki will shut down!

Please note that this wiki will be made read-only and eventually be taken offline.

A replacement is being discussed at We'd be happy to have more input for that discussion and volunteers who help us migrate the content of this wiki to its replacement.

Change keyboard maps

From awesome
Jump to: navigation, search

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[edit]

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

setxkbmap method[edit]

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[edit]

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] )

 -- Mouse bindings
 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

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 :

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

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 )
    -- Mouse bindings
        awful.button({ }, 1, function () kbdcfg.switch() end)

Enabling accelerator keys[edit]

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[edit]

Example global keybindings to switch layouts:

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

External solutions[edit]

setxkbmap method[edit]

This unfortunately will make the run prompt unusable as long as you're not using the first layout, see task 1053

Configure your X server with ~/.xinitrc or similar by adding the following line:

 setxkbmap us,ru

KDE 4 method[edit]

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.

How to test keyboard[edit]

The keycodes used by Xorg are reported by a utility called xev, which is provided by the xorg-xev package. Of course to execute xev, you need to be in a graphical environment, not in the console. With the following command you can start xev and show only the relevant parts:

 $ xev | grep -A2 --line-buffered '^KeyRelease' | sed -n '/keycode /s/^.*keycode \([0-9]*\).* (.*, \(.*\)).*$/\1 \2/p'

Here is an example output:

 108 Meta_R
 62 Shift_R
 13 4
 14 5
 36 Return

If you press a key and nothing appears in the terminal, it means that either the key does not have a scancode, the scancode is not mapped to a keycode, or some other process is capturing the keypress. If you suspect that a process listening to X server is capturing the keypress, you can try running xev from a clean X session:

 $ xinit /usr/bin/xterm -- :1