Take the 2-minute tour ×

I want to bind a key such that a particular application (in my case Pidgin or Calculator) gets the focus, then when I am done and press key again, the focus returns to the previously focused application, as though I never changed focus.

For example:


Chromium —Alt+Tab→ Files —Alt+Tab→ Chromium

Use of key binding:

Chromium —(bound key)→ Pidgin (answer to chat) or Calc (calculate something) —(bound key)→ Chromium

After using above:

Chromium —Alt+Tab→ Files (not Pidgin) —Alt+Tab→ Chromium

Other alternatives are welcome.

share|improve this question
You mean you want to replace the shortcut ALT + Tab by another one? Just tried it, but doesn't work for me either .. –  Bertram Nudelbach Aug 5 '13 at 20:58
add comment

1 Answer

Toggling between two applications can be done, as you know, with Alt+Tab. But it's designed go through all of the open windows by Z-order; so every time a third application is activated, the Z-order changes and it's usually annoying to get the two apps you're mainly interested back in sync for switching.

I read your requirement to be that you have a main application Chromium and a secondary application, let's go with gnome-calculator, that you switch to frequently and would like a foolproof toggle between them using a single keybinding.

You can, however, create this behavior with a script and a custom keybinding. To create the script, I wanted something simple (for me) to understand and that could be easily modified without compiling; so I used python.

import gtk
import re
import sys
import time
import wnck

#windows are found through the titlebar, not the app name
#edit the titles to change the apps
titlePrimaryApp = re.compile(".*Chromium*")
titleSecondaryApp = re.compile("Calculator")

screen = wnck.screen_get_default()

#needed for the code that gets the windows list to work
while gtk.events_pending():

active_window = screen.get_active_window()
open_windows = screen.get_windows()

#if the primary app is already activated, find the secondary app, and activate it
if titlePrimaryApp.match(active_window.get_name()):
  for w in open_windows:
    if titleSecondaryApp.match(w.get_name()):

#otherwise, activate the primary app
  for w in open_windows:
    if titlePrimaryApp.match(w.get_name()):

To use this script:

  1. Save it in a file such as myswitcher.py. A good place for it is /home/<user>/bin.
  2. Make it executable: chmod +x myswitcher.py
  3. You will need: sudo apt-get install python-wnck. This is the library that works with Gtk windows. It is not installed by default.
  4. Link the script to a keybinding.

    The easiest way to do this is to create a custom shortcut in System Settings >> Keyboard >> Shortcuts >> Custom Shortcuts.

  5. After highlighting Custom Shortcuts in the list on the left, click the plus sign to add a shortcut.

  6. In the popup window, choose a name for the shortcut and enter the command /path/to/myswitcher.py.

  7. The new shortcut will be added to the list of shortcuts in the window. To the right of the name will be the word disabled. Set the keybinding by clicking the word disabled, then typing the key combination that you want.

  8. Make sure you choose a keybinding that doesn't conflict with other programs. Those bindings are set at a lower level and will preempt the script. I chose Ctrl+ \ (backslash) since it is not used by anything else and is more or less the mirror image of Alt+Tab (at least on my keyboard layout).

  9. You need to log out and log back in before the custom keybinding will work.

Credit for help with the script:

I had most of the script logic worked out before looking around for examples (and there's no shortage of wnck examples out there); but I need to credit this answer on StackOverflow that solved some problems I was having with the regular expressions usage, the syntax for passing in the gtk timestamp, and most importantly providing the key lines about the Gtk events. Without those 2 lines, get_windows returns an empty list.

share|improve this answer
add comment

Your Answer


