Which window has current focus?

12,542

Solution 1

What you want is libwnck (if you're just interested in windows) or libbamf (if you're interested in windows and the applications that own them).

Solution 2

Another thing you can use is xdotool:

xdotool getwindowfocus

would return the Window ID of the focussed window, and:

xdotool getwindowfocus getwindowname

would tell you its name.

Solution 3

try using the wnck lib and then use this code:

import wnck
import gtk

while True:
if __name__ == '__main__':
    screen = wnck.screen_get_default()
    screen.force_update()
    while True:
        while gtk.events_pending():
            gtk.main_iteration()
        #time.sleep(0.5)
        print screen.get_active_window().get_name()

Solution 4

If you're happy doing a little X11 programming, then the EWMH spec is what you're after - specifically _NET_ACTIVE_WINDOW.

Solution 5

Well if you can ping something back to the shell:

xdpyinfo | grep focus

Should work.

Edit: For slightly cleaner output, try this:

xdpyinfo | grep -Eo 'window 0x[^,]+' | cut -d" " -f2
Share:
12,542

Related videos on Youtube

Erigami
Author by

Erigami

Updated on September 17, 2022

Comments

  • Erigami
    Erigami over 1 year

    I'd like to know (programmatically) which window has current focus. Is there a window-manager independent way of discovering that?

    Otherwise, how does one determine which window has focus in Compiz or Metacity?

  • Oli
    Oli over 13 years
    I get xdotool: Unknown command: getwindowname for the second one.
  • frabjous
    frabjous over 13 years
    You're using an outdated version of xdotool. The one in the Ubuntu repos is positively ancient, for example. There's a more recent .deb file at their site, for certain architectures, or compile from source if need be.
  • Oli
    Oli over 13 years
    Not sure about "positively ancient"... My version is 2.20100701.2961, current stable is 2.20101012.3049. 3 months usually doesn't mean that much... But if that's the case, so be it.
  • frabjous
    frabjous over 13 years
    I was thinking the one in the Lucid LTS repos; Maverick isn't so bad, but apparently it makes a difference here.
  • Erigami
    Erigami over 13 years
    Thanks! This is exactly what I was looking for. It lead me to another question, which was exactly what I was looking for.