Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am using archlinux in combination with awesome-wm for quite a while now. I still haven't figured out how to automatically grab the focus on an application when changing workspace through Mod+number.

For example, I have firefox running on workspace 2 and some terminals on workspace 1. If I am in workspace 2 and press Mod+1 to go to workspace 1, no terminal is focussed. I can ofcourse click on a terminal to grab focus, or press Mod+j or Mod+k, but I would like this to happen automatically.

How can I make this possible? Thanks in advance!

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

I figured it out so I will answer my own question. This might not be the best or most efficient way, but it gets the job done. This question might fit more on stackoverflow since it is more focussed on programming.

I wrote this function, which grabs focus on the first client on screen (except for Conky, the system monitor I use).

-- Grab focus on first client on screen
function grab_focus()
    local all_clients = client.get()
    for i, c in pairs(all_clients) do
        if c:isvisible() and c.class ~= "Conky" then
            client.focus = c
        end
    end
end

I now call this function in rc.lua where the numbered keys are mapped:

-- Bind all key numbers to tags.
...
if tags[screen][i] then
    awful.tag.viewonly(tags[screen][i])
    grab_focus()
end
share|improve this answer
add comment

Thank you very much Carlito, I've been struggling around the whole afternoon to solve this problem, and your function works perfectly.

I just call it by itself "grab_focus()" into the keybinding "for" cycle I want to.

I use Awesome 3.5 with Shifty, this is how I fixed it (please notice "grab_focus()"):

-- Bind all key numbers to tags.
-- Be careful: we use keycodes to make it works on any keyboard layout.
-- This should map on the top row of your keyboard, usually 1 to 9.

   for i = 1, (shifty.config.maxtags or 9) do
    globalkeys = awful.util.table.join(globalkeys,
        awful.key({ modkey }, "#" .. i + 9,
                  function ()
                      awful.tag.viewonly(shifty.getpos(i))
                      grab_focus()
                end),
        awful.key({ modkey, "Control" }, "#" .. i + 9,
                  function ()
                      awful.tag.viewtoggle(shifty.getpos(i))
                  end),
        awful.key({ modkey, "Shift" }, "#" .. i + 9,
                  function ()
                      if client.focus then
                          local t = shifty.getpos(i)
                          awful.client.movetotag(t)
                          awful.tag.viewonly(t)
                       end
                  end),
        awful.key({ modkey, "Control", "Shift" }, "#" .. i + 9,
                  function ()
                      if client.focus then
                          awful.client.toggletag(shifty.getpos(i))
                      end
                  end))
end
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.