Which window has current focus?
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
Related videos on Youtube
Erigami
Updated on September 17, 2022Comments
-
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 over 13 yearsI get
xdotool: Unknown command: getwindowname
for the second one. -
frabjous over 13 yearsYou'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 over 13 yearsNot 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 over 13 yearsI was thinking the one in the Lucid LTS repos; Maverick isn't so bad, but apparently it makes a difference here.
-
Erigami over 13 yearsThanks! This is exactly what I was looking for. It lead me to another question, which was exactly what I was looking for.