Detect if class that inherit Component is in focus

Hi,

in my project I inherited Component in my class Editor and in my “EditorManager”, that is a class that collects in an OwnedArray instances of Editor, I want to store in a variable Editor* focusedEditor the Editor* in screen that has currently the focus.

to do this in a timerCallback i wrote this code:

for (Editor* editor : editors)
    {
        if (editor == dynamic_cast<Editor*>(juce::Component::getCurrentlyFocusedComponent()))
        {
            focusedEditor = editor;
        }
    }

but it fails ever…
The logic is that when is focus that does’t is an Editor, but only a Component, the var focusedEditor is nullptr, while when some Editor object is in focus it points to this one…

How can I do? :pray:

I’m not sure if I fully understand your post, but you might want to override Component::focusGained() and focusLost() to keep track of which of your editors is focused.

It’s not uncommon for getCurrentlyFocusedComponent() to return nullptr depending on what you are clicking on, or whether you bring some other app to the front and then go back to your app…

1 Like

wow, really really thank you!