How to create a push button that works with spacebar?

Also posted on github.com/gtk-rs/gtk4-rs/discussions

I created a push button using GestureClick:

example1

Here is a snippet of the code:

    // --snip--

    // Add an action when the button is pressed
    gesture_click.connect_pressed(
        clone! (@weak push_button => move |gesture, _n_press, _x, _y| {
            gesture.set_state(gtk::EventSequenceState::Claimed);
            push_button.set_label("Release me!");
            print!("Button pressed!  \r");
            let _ = std::io::stdout().flush();
        }),
    );

    // Add an action when the button is released
    gesture_click.connect_released(
        clone! (@weak push_button => move |gesture, _n_press, _x, _y| {
            gesture.set_state(gtk::EventSequenceState::Claimed);
            push_button.set_label("Press me!");
            print!("Button released!\r");
            let _ = std::io::stdout().flush();
        }),
    );

    // --snip--

But I can’t figure out how to get this to work with the space bar. Currently, this is what happens using the spacebar:

example2

I also tried working with the spacebar using EventControllerKey, but I ran into the same situation as described here:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.