# How to make parent Component dynamically catch MouseEvent instead of child components?

**URL:** <https://forum.juce.com/t/how-to-make-parent-component-dynamically-catch-mouseevent-instead-of-child-components/57098>\
**Category:** General JUCE discussion\
**Created:** [July 20, 2023, 7:46am UTC](https://forum.juce.com/t/how-to-make-parent-component-dynamically-catch-mouseevent-instead-of-child-components/57098 "2023-07-20T07:46:02Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kraku](https://avatars.discourse-cdn.com/v4/letter/k/74df32/32.png) [@Kraku](https://forum.juce.com/u/Kraku)\
**Post date:** [July 20, 2023, 7:46am UTC](https://forum.juce.com/t/how-to-make-parent-component-dynamically-catch-mouseevent-instead-of-child-components/57098/1 "2023-07-20T07:46:02Z")

</div>

I’d like a parent Component to catch all MouseEvents which happen when right mouse click happens. All the rest should go to child components.

Is there an easy way to do this?

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [July 20, 2023, 8:46am UTC](https://forum.juce.com/t/how-to-make-parent-component-dynamically-catch-mouseevent-instead-of-child-components/57098/2 "2023-07-20T08:46:25Z")

</div>

There are three ways:

1. let the parent component catch all and call the child component, if it doesn’t want to handle it:

```auto
ParentComponent()
{
    // keep clicks for children to yourself
    setInterceptsMouseClicks (true, false);
}
// you need to implement this for all mouse events the children need
void mouseDown (const MouseEvent& event) override
{
    if (event.mods.isRightButton())
    {
        doYourThing();
        return;
    }
    for (auto* child : getChildren())
    {
        if (child->hitTest (event.x, event.y))
        {
           child->mouseDown (event->getRelativeTo (child));
           return;
        }
    }
}

```

1. Add a mouse listener to every child  
This means, you need to modify every child to not react on right click

2. have every child call the method of the parent via a back link you added on construction.  
This only works for custom components

Well, none of those qualifies for “easy way”, but it is doable.  
It should be possible to create a generic event filter for option one, but that needs some thought…

---

<div class="post-metadata">

**Author:** ![Kraku](https://avatars.discourse-cdn.com/v4/letter/k/74df32/32.png) [@Kraku](https://forum.juce.com/u/Kraku)\
**Post date:** [July 20, 2023, 9:21am UTC](https://forum.juce.com/t/how-to-make-parent-component-dynamically-catch-mouseevent-instead-of-child-components/57098/3 "2023-07-20T09:21:11Z")

</div>

The first option looks promising. However, I got some very weird behavior which I cannot explain:

Nothing seemed to change.  
The parent mouseDown() doesn’t get called at all for some reason and the child component’s mouseDown() still gets called as usual.

In other words, with the changes you suggested, nothing happened. The component(s) seem to be working exactly as before as the parent’s mouseDown() doesn’t get called for some weird reason.

I couldn’t find any other place which would set setInterceptsMouseClicks() for this parent component, so I doubt it’s that what could be causing this issue now.

In my current parent Component the child components cover the whole parent surface area completely. Still this behavior should not happen if I understand correctly how the  
setInterceptsMouseClicks(true, false) should make things behave.

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [July 20, 2023, 9:24am UTC](https://forum.juce.com/t/how-to-make-parent-component-dynamically-catch-mouseevent-instead-of-child-components/57098/4 "2023-07-20T09:24:44Z")

</div>

> [@Kraku](#):
>
> I couldn’t find any other place which would set setInterceptsMouseClicks() for this parent component, so I doubt it’s that what could be causing this issue now.

Is the parent derrived from juce::Component?  
Maybe you can add a breakpoint in setInterceptsMouseClicks or in mouseDownInternal to see, why it doesn’t respect the flag to skip the children…

---

<div class="post-metadata">

**Author:** ![Kraku](https://avatars.discourse-cdn.com/v4/letter/k/74df32/32.png) [@Kraku](https://forum.juce.com/u/Kraku)\
**Post date:** [July 20, 2023, 9:30am UTC](https://forum.juce.com/t/how-to-make-parent-component-dynamically-catch-mouseevent-instead-of-child-components/57098/5 "2023-07-20T09:30:48Z")

</div>

The parent is derived from another class which is derived from a Component.

I tried making a hole in the parent component so I can actually click it and added the following lines to the parent’s mouseDown():

```auto
        bool first, second;
        getInterceptsMouseClicks(first, second);

```

The results I got were:

```auto
first == true
second == false

```

So the flags are correct.  
I’ll see if I can get some answers by following your debugging suggestion by placing those break points.

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [July 20, 2023, 9:35am UTC](https://forum.juce.com/t/how-to-make-parent-component-dynamically-catch-mouseevent-instead-of-child-components/57098/6 "2023-07-20T09:35:25Z")

</div>

And another one to check: being a child is not defined by being a class member, but rather where you called addAndMakeVisible. I have seen things like:

```auto
thirdparty.addAndMakeVisible (child);

```

This way the event would skip `this` and be passed from thirdparty directly to the child…

---

<div class="post-metadata">

**Author:** ![Kraku](https://avatars.discourse-cdn.com/v4/letter/k/74df32/32.png) [@Kraku](https://forum.juce.com/u/Kraku)\
**Post date:** [July 20, 2023, 9:44am UTC](https://forum.juce.com/t/how-to-make-parent-component-dynamically-catch-mouseevent-instead-of-child-components/57098/7 "2023-07-20T09:44:55Z")

</div>

All components seem to be added the following way:

addAndMakeVisible(&childComponent);

So they’re added by / into the same component which owns them. So no third party bugs seem to be present. At least I couldn’t find one.

mouseDownInternal() seems to be related only to treeview or something and isn’t called when mouse is clicked anywhere in the application. So no point debugging that.

EDIT:  
Ah, you must be referring to internalMouseDown() which does call mouseDown(). Debugging now.

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [July 20, 2023, 10:04am UTC](https://forum.juce.com/t/how-to-make-parent-component-dynamically-catch-mouseevent-instead-of-child-components/57098/8 "2023-07-20T10:04:43Z")

</div>

> [@Kraku](#):
>
> Ah, you must be referring to internalMouseDown() which does call mouseDown(). Debugging now.

Oh sorry, typed from memory, should have opened the sources 😊

---

<div class="post-metadata">

**Author:** ![Kraku](https://avatars.discourse-cdn.com/v4/letter/k/74df32/32.png) [@Kraku](https://forum.juce.com/u/Kraku)\
**Post date:** [July 20, 2023, 12:37pm UTC](https://forum.juce.com/t/how-to-make-parent-component-dynamically-catch-mouseevent-instead-of-child-components/57098/9 "2023-07-20T12:37:19Z")

</div>

The below function(s) seems to be the ones JUCE uses to solve which components are under the mouse cursor at any given time.

detail::ComponentHelpers::hitTest eventually calls Component::hitTest() which I’ve listed below.

```auto
Component* Component::getComponentAt (Point<float> position)
{
    if (flags.visibleFlag && detail::ComponentHelpers::hitTest (*this, position))
    {
        for (int i = childComponentList.size(); --i >= 0;)
        {
            auto* child = childComponentList.getUnchecked (i);

            child = child->getComponentAt (detail::ComponentHelpers::convertFromParentSpace (*child, position));

            if (child != nullptr)
                return child;
        }

        return this;
    }

    return nullptr;
}

bool Component::hitTest (int x, int y)
{
    if (! flags.ignoresMouseClicksFlag)
        return true;

    if (flags.allowChildMouseClicksFlag)
    {
        for (int i = childComponentList.size(); --i >= 0;)
        {
            auto& child = *childComponentList.getUnchecked (i);

            if (child.isVisible()
                 && detail::ComponentHelpers::hitTest (child, detail::ComponentHelpers::convertFromParentSpace (child, Point<int> (x, y).toFloat())))
                return true;
        }
    }

    return false;
}

```

As far as I can tell, the core of the issue is the following:

`Component* Component::getComponentAt (Point<float> position)`

If the component has been set to intercept the mouse clicks, then also all its child components will always be tested, regardless of if the component has been set to not to do that.

In other words, the hierarchical test to find the child nodes that touch the mouse cursor does NOT stop when component has been marked to NOT allow child nodes to be tested IF/WHEN that same node has been marked to react to mouse clicks.

Could someone else also confirm my logical analysis if this is true?  
If it is, then this sounds like JUCE has a bug.

---

<div class="post-metadata">

**Author:** ![Marcusonic](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@Marcusonic](https://forum.juce.com/u/Marcusonic)\
**Post date:** [July 20, 2023, 10:41pm UTC](https://forum.juce.com/t/how-to-make-parent-component-dynamically-catch-mouseevent-instead-of-child-components/57098/11 "2023-07-20T22:41:53Z")

</div>

I’m not sure but maybe the easiest way is to process the events normally in the child, and if they are right click events call the parent events with getParentComponent()-\>mouseDown(event); (you may have to adjust the relative coordinates of the event if the position is needed)

---

<div class="post-metadata">

**Author:** ![Kraku](https://avatars.discourse-cdn.com/v4/letter/k/74df32/32.png) [@Kraku](https://forum.juce.com/u/Kraku)\
**Post date:** [July 21, 2023, 8:54am UTC](https://forum.juce.com/t/how-to-make-parent-component-dynamically-catch-mouseevent-instead-of-child-components/57098/12 "2023-07-21T08:54:23Z")

</div>

I wonder if there would be an easy way to somehow put another invisible Component on top of the actual component, which I want to have that special behavior with right mouse button. Then that invisible component would have a pointer to that actual component and would forward the mouse messages to the component as needed.

Actually the code in the first answer to this thread might do the trick if the Components were used the way I described here. I’ll try this combination and see what happens.

---

<div class="post-metadata">

**Author:** ![Kraku](https://avatars.discourse-cdn.com/v4/letter/k/74df32/32.png) [@Kraku](https://forum.juce.com/u/Kraku)\
**Post date:** [July 21, 2023, 10:35am UTC](https://forum.juce.com/t/how-to-make-parent-component-dynamically-catch-mouseevent-instead-of-child-components/57098/13 "2023-07-21T10:35:25Z")

</div>

Well, the idea was good, but it didn’t work very well in practice:

Some of the ComboBoxes inside those child components don’t react for mouse clicks.  
Also rotary sliders don’t like it when mouse cursor goes outside the area of the covering Component. Meaning that the slider rotates only as long as the mouse cursor (which is invisible at that time) stays inside the covering component. Once it goes outside, the knob/slider stops rotating.

I’ll go the most obvious way then, which was suggeseted here already, and just implement the functionality to each of the child components.

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [July 21, 2023, 10:42am UTC](https://forum.juce.com/t/how-to-make-parent-component-dynamically-catch-mouseevent-instead-of-child-components/57098/14 "2023-07-21T10:42:35Z")

</div>

> [@Kraku](#):
>
> Also rotary sliders don’t like it when mouse cursor goes outside the area of the covering Component. Meaning that the slider rotates only as long as the mouse cursor (which is invisible at that time) stays inside the covering component. Once it goes outside, the knob/slider stops rotating.

You might be able to fix that setting

```auto
event.source.enableUnboundedMouseMovement (true, false);

```

But it’s a rabbit hole. You also interfere with the sequence of mouseEnter/mouseExit and such, so I expect more edge cases arising.

---

<div class="post-metadata">

**Author:** ![Kraku](https://avatars.discourse-cdn.com/v4/letter/k/74df32/32.png) [@Kraku](https://forum.juce.com/u/Kraku)\
**Post date:** [July 21, 2023, 1:19pm UTC](https://forum.juce.com/t/how-to-make-parent-component-dynamically-catch-mouseevent-instead-of-child-components/57098/15 "2023-07-21T13:19:05Z")

</div>

Indeed. That’s why I’ll just bite the bullet and implement the right mouse button handling into the child objects instead of trying to get it working on some on-top-of-Component.
