#!/usr/bin/python

import pyatspi

def onCaretMoved(event):
    print event
    text = event.source.queryText()
    print "text.caretOffset", text.caretOffset
    for i in xrange(text.getNSelections()):
        print "  selection", text.getSelection(i)
    print

pyatspi.Registry.registerEventListener(onCaretMoved,
                                       "object:text-caret-moved")

pyatspi.Registry.start(async=False)
