#!/usr/bin/env python3

from gi.repository import Gtk, Gdk, GLib

def print_event(win, event):
    if event.type == Gdk.EventType.KEY_PRESS:
        if event.keyval == Gdk.KEY_Escape:
            Gtk.main_quit()
        print(event.type, event.keyval)
    elif event.type == Gdk.EventType.KEY_RELEASE:
        print(event.type, event.keyval)

def asdf():
    print("grabbing")
    widget.grab_focus()
    widget.grab_add()

win = Gtk.Window()

widget = Gtk.Button('quit')
widget.props.can_focus = True

widget.connect('event', print_event)
widget.connect('activate', lambda *args: Gtk.main_quit())
win.add(widget)
win.show_all()

GLib.timeout_add(5000, asdf)

Gtk.main()
