Mouse click triggers twice for parent


#1

First of all, here is my code:

Parent constructor:

    addMouseListener(this, true);

and then still in parent:

   if (event.mouseWasClicked()) {
	if (event.mods == juce::ModifierKeys::rightButtonModifier) {
             if (event.eventComponent == this) {
                      // Do-stuff
             }
        }
   }

What I know:
Since I pass true in addMouseListener(), parent will also receive mouseevents for child components.

What I expected:
Mouse event triggers once for the parent component and once for the child component.

What actually happens and confuses me:
Mouse event triggers twice just for the parent and once for the child.

Obvious question:
What causes this behavior and how can I “fix” it?


#2

The Component class is already a MouseListener so when you call addMouseListener() in your parent component you’re adding it twice, hence the two mouse events. Just override the virtual mouseDown/Up/Drag etc. methods of Component to be notified of mouse events inside the parent. If you want to know about mouse events in a child component then you can add your parent as a MouseListener to the child component by calling addMouseListener() on it.

Ed