#!/usr/bin/python

# Steps to reproduce:
# 1. Start this listener in a terminal.
# 2. Launch gtk3-demo's Printing demo.
# 3. Press Tab to move focus from the General tab to the printer table.
# 4. Press Ctrl+Alt+Page Down to move to the next notebook page.
# 5. Press Shift+Tab to move focus from the Pages per side combo box
#    Up to the Page Setup tab.
#
# Expected results: The General tab would not claim focus.
# Actual results: The General tab does claim focus.

import pyatspi

def onFocused(e):
    try:
        appName = e.host_application.name
    except :
        return
    if appName != 'gtk3-demo':
        return

    if not e.detail1:
        return

    print e

def onKey(e):
    typeString = '(unknown)'
    if e.type == pyatspi.KEY_PRESSED_EVENT:
        typeString = '>>> KEY PRESSED'
    elif e.type == pyatspi.KEY_RELEASED_EVENT:
        typeString = '<<< KEY RELEASED'
    print '%s: %s' % (typeString, e.event_string)

pyatspi.Registry.registerEventListener(onFocused, 'object:state-changed:focused')
pyatspi.Registry.registerKeystrokeListener(
    onKey, mask=range(256), kind=(pyatspi.KEY_PRESSED_EVENT, pyatspi.KEY_RELEASED_EVENT))
pyatspi.Registry.start()
