from gi.repository import Gdk def on_click(window, event): if event.button == Gdk.BUTTON_PRIMARY: print("Left click detected") elif event.button == Gdk.BUTTON_SECONDARY: print("Right click detected") window = Gdk.Window() window.connect("button-press-event", on_click)
from gi.repository import Gdk def on_key_press(window, event): if event.keyval == Gdk.KEY_a: print("a key was pressed") elif event.keyval == Gdk.KEY_b: print("b key was pressed") window = Gdk.Window() window.connect("key-press-event", on_key_press)Similar to the previous example, a Gdk Window object is created and a function on_key_press is defined which takes in the window object and the event object. The event object contains information about the key that was pressed, including the key code (keyval). The connect method is used to associate the on_key_press function with the "key-press-event".