Window Focus

Does anybody know how to get a message or trigger events based on a window coming into focus?

I can trigger an event based on a component being focused by a mouse down on the component, but not the component window.

So if I am shuffling around open windows by the title bar of the ui, my UI components have no clue if their window was the last selected or focused on an operating system level.

I’ve tried, showing, keyboard focus, as well as grabbing the window handle, and comparing mouse positions on a timer (sort of works) , but I can’t seem to distinguish between local windows within a process such as a DAW.

Maybe some sort of operating system relative z order through the component peer or tope level window?

There’s got to be a something deep in the depths of the juce framework for this.
If anyone has any ideas or a good direction to try, please let me know.


I have wrestled with this kind of thing as well. Are you talking about inside a plugin?

Yessir, thanks for linking that forum I didn’t see that one. This is a rabbit hole for sure.

Have you found any functions that work?
I feel like I’ve tested almost everything I can think of.

I found this code from this thread, some what effective.

        auto topComp = this->getTopLevelComponent();

        if (topComp != nullptr)
            ComponentPeer* peer = topComp->getPeer();
            if (peer)
                const Point<float> point = peer->localToGlobal (Point<float>());

                if (point != previousPoint)
                    previousPoint = point;


                std::cout << "focus gained by " + juce::String((int)localID) << std::endl;
                std::cout << "focus lost by " + juce::String((int)localID) << std::endl;

I have not tried that approach; what I have at the moment is something like this, which may not be applicable to what you want to accomplish:

I’ve found juce::Process::isForegroundProcess() will return true as long as the DAW is selected.

I’m going to try a few more things, I’ll post here with what I find.

I’m going to try and wrap that process, maybe the one I shared, and then testing the top left pixel and bottom right pixel of the desktop’s component location.