Keyboard focus when opening a plugin in the DAW

I’ve been experimenting with the following approach. I can’t say it is extensively tested in many hosts because I haven’t released anything yet. But I was having the same problem, and I want my MainComponent to grab focus when the plugin window is first shown.

The idea is: When the MainComponent is created, instantiate a Timer class that checks periodically (i.e. once per second) for the window being the frontmost process, and then focuses the MainComponent, after which it stops the timer callback.

I do it with a separate dynamically allocated class derived from Timer, but you could also have your MainComponent inherit from Timer, i.e.:

In timerCallback():

void MainComponent::timerCallback()
{
    if (Process::isForegroundProcess())
    {
        if (Component::getCurrentlyFocusedComponent() != this)
            grabKeyboardFocus();

        // it's a one-shot, stop it the first time we're a foreground process
        stopTimer();
    }
}

I’m not sure if this whole idea is dumb, but it seems to work with limited testing…so far…

1 Like