Thursday, January 3, 2013

Keyboard switcher for awesome 4.5+

So there was Awesome update recently, which totally changed syntax of rc.lua (or at least how Awesome uses this language). I have very little changes in rc.lua, mainly widget for changing keyboard layouts.

What I did is that I deleted my old ~/.config/awesome/rc.lua, copied the default one (on Arch linux it is in /etc/xdg/awesome/rc.lua) and recreated widget for switching keyboard layouts.

So here's code of widget:
-- keyboard switcher
-- Keyboard map indicator and changer
    kbdswitcher = {}
    kbdswitcher.cmd = "setxkbmap"
    kbdswitcher.layout = { { "us", "" }, { "cz", "qwerty" } }
    kbdswitcher.current = 1  -- us is our default layout
    kbdswitcher.widget = wibox.widget.textbox()
    kbdswitcher.widget:set_text(" " .. kbdswitcher.layout[kbdswitcher.current][1] .. " ")
    kbdswitcher.switch = function ()
       kbdswitcher.current = kbdswitcher.current % #(kbdswitcher.layout) + 1
       local t = kbdswitcher.layout[kbdswitcher.current]
       kbdswitcher.widget:set_text(" " .. t[1] .. " ")
       os.execute( kbdswitcher.cmd .. " " .. t[1] .. " " .. t[2] )
    end 
-- Mouse bindings
kbdswitcher.widget:buttons(awful.util.table.join(
    awful.button({ }, 1, function () kbdswitcher:switch() end)
))
you can just copy&paste this into your rc.lua (I have it under definition of mytextclock widget) and it should do the trick.

Don't forget to add keyboard switcher to your widgets to display, I have it after clock widget in right corner:
right_layout:add(mytextclock)
right_layout:add(kbdswitcher.widget)
And if you want to use keyboard shortcut in addition to mouse to switch layouts, add this to globalkeys array:
-- Alt + Right Shift switches the current keyboard layout
awful.key({ "Mod1" }, "Shift_L", function () kbdswitcher:switch() end),
I hope this will be useful to anyone ^_^

PS: yes, for those of you who noticed, the widget is not my by origin, I merely updated it to work under awesome 4.5

1 comment: