import pyatspi

def onEvent(e):
    print e
    states = e.source.getState().getStates()
    print "       ", states
    if pyatspi.STATE_FOCUSED in states:
        print "^^^ OK!"
    else:
        print "^^^ Focus event received, but STATE_FOCUSED not set!"

pyatspi.Registry.registerEventListener(onEvent, "focus")
pyatspi.Registry.start(async=False)
