GTK4: React to the release of a mouse click

Hello,
I read the book on GTK4 and for my application I would like to react to the release of the mouse key so I think I want to listen to the button-release-event (at least that’s what I found in the GTK3 docs). From the book I adapted the example and tried this:

button
        .connect_local("button-release-event", false, move |args| {
            // Get the button from the arguments
            let button = args[0]
                .get::<Button>()
                .expect("The value needs to be of type `Button`.");
            // Set the label to "Hello World!" after the button has been clicked on
            button.set_label("Hello World!");
            None
        })
        .expect("Could not connect to signal.");

It’s not working.
I was unable to find how the release can be listened to with GTK4. In the GTK3 docs it says I need to enable the #GDK_BUTTON_RELEASE_MASK. I can’t find anything on masks in the GTK4 docs. I also looked in the docs of the Rust bindings. Can anyone help me and tell me how I can know when the user released the mouse key under GTK4?

Forget all you read about input events like that and “event masks”. Just add a GtkGestureClick to your widget and connect to its ::pressed or ::released signal.

Great, that was just the help I needed. Now I got it to work. Maybe this should be part of the GTK4 + Rust book so that beginners like me are not pointed to a wrong direction. This is how I solved it now:

    let gesture_click = gtk4::GestureClick::new();
    button.add_controller(&gesture_click);

    gesture_click.connect_pressed(move |_gesture, _n_press, _x, _y| {
        println!("pressed");
    });

    gesture_click.connect_released(move |_gesture, _n_press, _x, _y| {
        println!("released");
    });

Indeed it is more complicated. Learning about the new GTK4 gestures is easy, but for more complicated use cases you may notice that gestures just do not work. Then we have to fall back to the legacy events. Legacy sounds really not that nice, so I hesitated, but Mr Clasen confirmed that it is OK to use them. See

GtkGesture for CAD applications?

GitHub - StefanSalewski/SDT: A simple Nim GTK4 drawing and design tool

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