[FR] Buttons: add option to ignore clicks with right mouse button


#1

What I wish to do is have a Button display a context menu when right-clicked.

A MouseListener attached to it takes care of displaying the PopupMenu just fine.

The problem is that the Button still receives its “click” event when the right mouse button acts upon it, which is something I don’t want in my use case.

Would it be possible to add an option to the base Button class to avoid that?

EDIT: if it is of any help, this is the patch with which I have hardcoded that behavior (left-click only) in my local JUCE repo. It is provided only to show the sole three lines that would require intervention: left-click-only.txt (1.9 KB)


Button right click confusion
#2

+1!


#3

+1.
I had to write a subclass just for this purpose.


#4

Surely that’s why we have the other clicked() method that provides the modifier keys?

https://docs.juce.com/develop/classButton.html#af57cd4eec484491dc536e05d75e556dd


#5

I’m not sure you understood our use case Jules. here is a example :

you have a large component that contains many children buttons of all kind (textButtons, toggleButtons, etc…)
you want the user to be able to right click anywhere on that parent component to open a contextual menu.
at the moment, if the right click happens over a button, you will get the contextual menu, but the underlying button will also get clicked (and we don’t want that)

so we were more looking for something like a Button::setIgnoreRightClicks() or an additional flag to setInterceptClicks(). for now we have to inherits the buttons class.

I ended up doing something like that :

template <typename ButtonType>
struct ButtonIgnoringRightClick : public ButtonType
{
    using ButtonType::ButtonType;

    void mouseDown (const MouseEvent& e) override
    {
        if (e.mods.isPopupMenu())
            return;

        ButtonType::mouseDown (e);
    }

    void mouseUp (const MouseEvent& e) override
    {
        if (e.mods.isPopupMenu())
            return;

        ButtonType::mouseUp (e);
    }
};

ButtonIgnoringRightClick<TextButton> myTextButton;