How to track button right-click?

I need to track if Gtk.Button is clicked using RMB. Does GTK have any way to do this?

Assuming you’re using GTK4, look at GestureClick. You can use gtk_gesture_single_set_button to only listen to the right click (I think it’s button number 3.)

I see you tagged this with python - GestureClick has the Python documentation.

1 Like

As I’ve read in documentation, I can’t assign a specific button to this GestureClick widget. So how should I listen that specific button?

GestureClick is inherited from GestureSingle
https://lazka.github.io/pgi-docs/Gtk-4.0/classes/GestureClick.html#signals
https://lazka.github.io/pgi-docs/Gtk-4.0/classes/GestureSingle.html#properties

Use set_button to specify the button number to listen to. The right button is usually number 3. I’m usually extra cautious and check the button number in the signal handler too, but that’s probably not needed.

I’m talking about specifying Gtk.Button to listen, not mouse button. There is a misunderstanding.

Attach the gesture to the Button. I thought you meant the mouse button as you refer to the RMB in your question.

        mouse_gesture = Gtk.GestureClick.new()
        mouse_gesture.set_button(3)    # watch only the right mouse button
        mouse_gesture.connect("released", button_release_signal_handler)
        the_button.add_controller(mouse_gesture)

Test this - I’ve never added a click controller to a button, only to other widgets. You’ll need to check to see if this interferes with the normal button click. Maybe a GTK dev can tell us.

2 Likes

This works well, thank you a lot!

No problem - once I understood what you were asking! Thanks for clarifying.

1 Like

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