How to identify the application that is currently focused?

I would like to be able to keep track of which application is currently focused on my Python X11 display. The goal is to bind it to a timetracking tool so that I can track how much time I spend on being unproductive.

I already found this code at http://thpinfo.com/2007/09/x11-idle-time-and-focused-window-in.html :

import Xlib.display display = Xlib.display.Display() focus = display.get_input_focus() print "WM Class: %s" % ( focus.focus.get_wm_class(), ) print "WM Name: %s" % ( focus.focus.get_wm_name(), ) 

However, this does not seem to work for me. Apparently, no matter which application is focused, both get_wm_class () and get_wm_name () just return None.

Of course, the solution should work with all of these new distorted window managers, such as Compiz, etc.

+6
python x11 xlib
source share
2 answers

Wow! I myself realized this:

 import Xlib.display display = Xlib.display.Display() window = display.get_input_focus().focus wmname = window.get_wm_name() wmclass = window.get_wm_class() if wmclass is None and wmname is None: window = window.query_tree().parent wmname = window.get_wm_name() print "WM Name: %s" % ( wmname, ) 
+8
source share

A slightly nicer solution, especially for a long application, rather than a script, would be to use libwnck to track the _NET_ACTIVE_WINDOW hint. (See the EWMH Specification for a clue.)

0
source share

All Articles