Awesome – Keyboard Layout Switch Shortcuts

awesomekeyboard shortcutskeyboard-layout

I use awesome as a window manager under gnome (debian system) and I made a widget to switch keyboard layout by this code:

-- Keyboard map indicator and changer
    kbdcfg = {}
    kbdcfg.cmd = "setxkbmap"
    kbdcfg.layout = { { "us", "" }, { "ru", "phonetic" } }
    kbdcfg.current = 1  -- us is our default layout
    kbdcfg.widget = widget({ type = "textbox", align = "right" })
    kbdcfg.widget.text = " " .. kbdcfg.layout[kbdcfg.current][1] .. " "
    kbdcfg.switch = function ()
       kbdcfg.current = kbdcfg.current % #(kbdcfg.layout) + 1
       local t = kbdcfg.layout[kbdcfg.current]
       kbdcfg.widget.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)
    ))
-- Alt + Right Shift switches the current keyboard layout
    awful.key({ "Mod1" }, "Shift_R", function () kbdcfg.switch() end),

It works, however there is a serious problem with keyboard behavior. When I switch to ru layout non of my keyboard bindings works. Awesome bindings don't work. For example vim shortcuts don't also work and etc. How I can solve this problem?

Best Answer

Your keyboard bindings are not working because your keyboard no longer has those keysyms.

If you want to have, for example, Alt+F do something, then you need "F" in some key. But if you load "ru" alone, then there is no "F" at all (nor any latin letter).

I think, specially when you need to handle multiple layouts, that it is much better to let X11 (through setxkbmap) do the job for you; the way the window managers do it is much more limited as you have discovered.

You may also try, as the awfull widget seems to call setxkbmap, to change "ru" with "ru,us", eg:

kbdcfg.layout = { { "us", "" }, { "ru,us", "phonetic" } }

that way, the "us" layout will be stacked on top of the "ru" one when loaded, and the keysysms of the "us" layout, while still not directly typeable, will be seen by the X11 layer that handles the key bindings.