Skip to content

Event controllers demo promotes bad practice for key modifiers #183

Open
@gjask

Description

@gjask

Event controller demo showcases button click with Ctrl key modifier by creating Gtk.EventControllerKey and tracking key state in global variable. This approach is at least bit quirky (eg. fails to track key state changes made off-focus) and seems like a bad practice (global variable, really?). I know it was meant to find show case for Gtk.EventControllerKey but I believe not teaching beginners (such as myself) bad practices is important. So maybe finding different show case is in order.

Proper technique for handling clicks and other events with key modifier is to use controller.get_current_event_state() in callback. For example like this.

def on_button_release(controller, clicks, x, y):
    if (controller.get_current_event_state() & Gdk.ModifierType.CONTROL_MASK):
        print("Ctrl was pressed")
    else:
        print("Ctrl was not pressed")

click = Gtk.GestureClick()
click.connect("released", on_button_release)
widget.add_controller(click)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions