Capturing child mouseEnter and mouseExit


I have a Component control which is composed of several child Component controls (buttons, a knob, and a label that all work together).

I want to perform special things when the mouse enters this composite control. So in other words, I want to capture mouseEnter and mouseExit for the enclosing component only. How would I do that? Do I need to add a mouse listener to each child? How would that work?


Have a look at the addMouseListener method - it has an option to catch events for all child components too, so you could use that to add a listener to the parent that will get all the events that happen inside it.


I’m using this but when I go into a child component, I get a mouseExit for the parent. I just one one mouseEnter/mouseExit for the whole set Components. Here’s what happens:

Parent -> contains Child

Mouse moves into parent -> Parent::mouseEnter()
mouse then moves into child -> Parent::mouseExit, Child::mouseEnter()
mouse leaves completely: Child::mouseExit(), Parent::mouseEnter(), Parent::mouseExit()

So, when I get Parent::mouseExit(), I have no way of knowing if it is because I went into a child, or if I am leaving the control. I can’t look into the future to know if I am going to get a mouseEnter() for the child.

What I would prefer is something like this:

Mouse moves into parent -> Parent::mouseEnter()
mouse then moves into child -> (no messages)
mouse leaves completely: Parent::mouseExit()

Or, some way to know in the mouseEnter() message callback, that I am still inside the parent?

Any ideas Jules?


You can just check to see if the mouse event location lies inside that component (i.e. thisComp->contains(Point(e.x,e.y))
[I believe the mouse event coordinates will already be in the correct space]

If it returns true, then it’s obviously still within its bounds, thus it must have entered a child.