#!/usr/bin/python

import pyatspi

COUNTERS = {}

def findParentPageTab(obj):
    return pyatspi.utils.findAncestor(lambda x: x.getRole() == pyatspi.ROLE_PAGE_TAB)

def onText(e):
    try:
        if e.host_application.name != "gtk3-demo":
            return
    except:
        return

    global COUNTERS
    count = COUNTERS.get(e.type, 0)
    COUNTERS[e.type] = count + 1

def onKeyPress(event):
    print("\nKEY PRESS: %s" % event.event_string)
    global COUNTERS
    COUNTERS = {}

def onKeyRelease(event):
    for counter, count in COUNTERS.items():
        print("%5i %s events" % (count, counter))
    print("KEY RELEASE: %s" % event.event_string)

pyatspi.Registry.registerKeystrokeListener(onKeyPress, kind=[pyatspi.KEY_PRESSED_EVENT])
pyatspi.Registry.registerKeystrokeListener(onKeyRelease, kind=[pyatspi.KEY_RELEASED_EVENT]) 
pyatspi.Registry.registerEventListener(onText, "object:text-caret-moved", "object:text-changed")
pyatspi.Registry.start()
