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

-- Keyboard map indicator and changer
kbdcfg = {}
kbdcfg.cmd = "setxkbmap"
kbdcfg.layout = { { "us", "" }, { "ru", "phonetic" } }
kbdcfg.current = 1  -- us is our default layout
kbdcfg.widget = wibox.widget.textbox()
kbdcfg.widget:set_text(" " .. kbdcfg.layout[kbdcfg.current][1] .. " ")
kbdcfg.switch = function ()
  kbdcfg.current = kbdcfg.current % #(kbdcfg.layout) + 1
  local t = kbdcfg.layout[kbdcfg.current]
  kbdcfg.widget:set_text(" " .. t[1] .. " ")
  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)

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

Warning.pngWarning
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.