# Get rid of right-click clicking

**URL:** <https://forum.juce.com/t/get-rid-of-right-click-clicking/23287>\
**Category:** Development\
**Created:** [July 11, 2017, 2:10pm UTC](https://forum.juce.com/t/get-rid-of-right-click-clicking/23287 "2017-07-11T14:10:22Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![hyakken](https://avatars.discourse-cdn.com/v4/letter/h/d9b06d/32.png) [@hyakken](https://forum.juce.com/u/hyakken)\
**Post date:** [July 11, 2017, 2:10pm UTC](https://forum.juce.com/t/get-rid-of-right-click-clicking/23287/1 "2017-07-11T14:10:22Z")

</div>

Hello,

I want to use right-clicking action only for showing popup menu.  
But when I do right-click on a textbutton, it triggers mouse clicking.  
How can I get rid of this behavior?

Thank you,

---

<div class="post-metadata">

**Author:** ![mibix](https://avatars.discourse-cdn.com/v4/letter/m/b2d939/32.png) [@mibix](https://forum.juce.com/u/mibix)\
**Post date:** [July 11, 2017, 2:13pm UTC](https://forum.juce.com/t/get-rid-of-right-click-clicking/23287/2 "2017-07-11T14:13:50Z")

</div>

You can check the modifier keys in the `buttonClicked()` method, and only respond to left click for action, and right click for menu. [https://www.juce.com/doc/classModifierKeys](https://www.juce.com/doc/classModifierKeys)

---

<div class="post-metadata">

**Author:** ![hyakken](https://avatars.discourse-cdn.com/v4/letter/h/d9b06d/32.png) [@hyakken](https://forum.juce.com/u/hyakken)\
**Post date:** [July 11, 2017, 2:27pm UTC](https://forum.juce.com/t/get-rid-of-right-click-clicking/23287/3 "2017-07-11T14:27:24Z")

</div>

Thank you, I will check it.

---

<div class="post-metadata">

**Author:** ![yfede](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/yfede/32/506_2.png) [@yfede](https://forum.juce.com/u/yfede)\
**Post date:** [July 12, 2017, 8:11am UTC](https://forum.juce.com/t/get-rid-of-right-click-clicking/23287/4 "2017-07-12T08:11:42Z")

</div>

Well, that’s only half of the story, because even with code that ignores actions taken with the right mouse button, the `Button` widget on screen will be animated as if it had been clicked, which is weird for a click that then does nothing.

I have modified slightly my version of JUCE so that the `Button` only reacts to left mouse clicks (required code changes are described below).

I think it would be a good **feature request** to have this behavior optionally enabled with a bool option in the base `Button` class, like `setClickableWithLeftMouseButtonOnly()`.  
What does @jules and its team think of this?

Here are the required code changes to only react to left clicks:  
(The original parts are those commented out, with their replacement next to them)

```
Button::ButtonState Button::updateState()
{
    /* return updateState (isMouseOver (true), isMouseButtonDown()); */
    return updateState (isMouseOver (true), isMouseButtonDown() && ModifierKeys::getCurrentModifiers ().isLeftButtonDown ());
}

```

* * *

```
void Button::mouseDown (const MouseEvent& e)
{
    updateState (true, /*true*/ e.mods.isLeftButtonDown ());

    if (isDown())
    {
        if (autoRepeatDelay >= 0)
            callbackHelper->startTimer (autoRepeatDelay);

        if (triggerOnMouseDown)
            internalClickCallback (e.mods);
    }
}

```

* * *

```
void Button::mouseDrag (const MouseEvent& e)
{
    const ButtonState oldState = buttonState;
    updateState (isMouseOrTouchOver (e), /*true*/ e.mods.isLeftButtonDown() || e.source.isTouch());

    if (autoRepeatDelay >= 0 && buttonState != oldState && isDown())
        callbackHelper->startTimer (autoRepeatSpeed);
}
```

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [July 12, 2017, 8:19am UTC](https://forum.juce.com/t/get-rid-of-right-click-clicking/23287/5 "2017-07-12T08:19:05Z")

</div>

We’re always reluctant to clutter a base class like Button with anything that’s not essential, and this doesn’t sound like a particularly useful addition for most people.

Why not just have a subclass which overrides mouseDown/Up/Drag() to only call the superclass method if the left button is down?

---

<div class="post-metadata">

**Author:** ![yfede](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/yfede/32/506_2.png) [@yfede](https://forum.juce.com/u/yfede)\
**Post date:** [July 12, 2017, 9:43am UTC](https://forum.juce.com/t/get-rid-of-right-click-clicking/23287/6 "2017-07-12T09:43:33Z")

</div>

> [@jules](#):
>
> Why not just have a subclass which overrides mouseDown/Up/Drag() to only call the superclass method if the left button is down?

It’s a good idea which would allow me to remove one of the places where I customized my copy of JUCE. Will have a go at it and let you know if it’s effective

---

<div class="post-metadata">

**Author:** ![hyakken](https://avatars.discourse-cdn.com/v4/letter/h/d9b06d/32.png) [@hyakken](https://forum.juce.com/u/hyakken)\
**Post date:** [July 13, 2017, 4:55pm UTC](https://forum.juce.com/t/get-rid-of-right-click-clicking/23287/7 "2017-07-13T16:55:59Z")

</div>

Thank you.  
This works perfectly for my purpose.

---

<div class="post-metadata">

**Author:** ![asimilon](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/asimilon/32/4235_2.png) [@asimilon](https://forum.juce.com/u/asimilon)\
**Post date:** [July 24, 2018, 1:03pm UTC](https://forum.juce.com/t/get-rid-of-right-click-clicking/23287/8 "2018-07-24T13:03:54Z")

</div>

How did you do with this? I’m struggling to find a way to do this for all the myriad button types without duplicating code unnecessarily, and I don’t want to get into hacking my local copy of JUCE either.

---

<div class="post-metadata">

**Author:** ![hyakken](https://avatars.discourse-cdn.com/v4/letter/h/d9b06d/32.png) [@hyakken](https://forum.juce.com/u/hyakken)\
**Post date:** [July 24, 2018, 4:52pm UTC](https://forum.juce.com/t/get-rid-of-right-click-clicking/23287/9 "2018-07-24T16:52:02Z")

</div>

Hello,

Sorry, I don’t remember this well.

I think I customized my copy of JUCE.

That’s all I can come up with now.

---

<div class="post-metadata">

**Author:** ![asimilon](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/asimilon/32/4235_2.png) [@asimilon](https://forum.juce.com/u/asimilon)\
**Post date:** [July 24, 2018, 7:43pm UTC](https://forum.juce.com/t/get-rid-of-right-click-clicking/23287/10 "2018-07-24T19:43:21Z")

</div>

I ended up with this approach :

```auto
    class LeftClickOnlyButton : public Button
    {
    public:
        LeftClickOnlyButton(const String& buttonName) : Button(buttonName) {};

        void internalClickCallback(const ModifierKeys& mods) override
        {
            if(mods.isLeftButtonDown())
            {
                Button::internalClickCallback(mods);
            }
        }
    };

    class LeftClickOnlyToggleButton : public ToggleButton
    {
    public:
        LeftClickOnlyToggleButton() : ToggleButton() {};
        LeftClickOnlyToggleButton(const String& buttonText) : ToggleButton(buttonText) {};

        void internalClickCallback(const ModifierKeys& mods) override
        {
            if(mods.isLeftButtonDown())
            {
                ToggleButton::internalClickCallback(mods);
            }
        }
    };

```

etc etc for the different button types I needed, but can’t help thinking there must be a more efficient way.

---

<div class="post-metadata">

**Author:** ![lalala](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/lalala/32/8577_2.png) [@lalala](https://forum.juce.com/u/lalala)\
**Post date:** [July 24, 2018, 9:59pm UTC](https://forum.juce.com/t/get-rid-of-right-click-clicking/23287/11 "2018-07-24T21:59:08Z")

</div>

here is another thread about it :

> [@\[FR\] Buttons: add option to ignore clicks with right mouse button](https://forum.juce.com/t/fr-buttons-add-option-to-ignore-clicks-with-right-mouse-button/20723/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 Butt…
