Keyboard focus when opening a plugin in the DAW

Hi,
is it possible to get the keyboard focus when a plugin window opens in the DAW, without clicking on the plugin?
I am trying to set some keyboard hotkeys for the plugin and would like these to work upon opening the plugin window in the DAW.
If yes, what would be the best practice to do this?
I’ve tried detecting the parent but it didn’t work and still need to click on the plugin to get keyboard focus.

Thanks!

TBH: I think this is a bug in the JUCE window wrapper class for AudioProcesserEditor. I implemented my own host and it is working without having to click into the window.
It is also not working when using the Standalone Plugin Wrapper. But I think I’m not running the very latest JUCE version :wink:

You might want to see this thread:

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

I think this approach is kind of your only option. The main problem is, that grabKeyboardFocus should be working before the component is added – a bit like a queue, that as soon as the component can grab focus, it gets it.

Currently the inner components have to relay on the outer components being programmed properly but it should be kind of the other way around.

Many thanks for the responses.
I tried the timer approach and it works but there is an issue when there more than one instance of the plugin open in the DAW. When the second instance opens, and both instances are open, they fight for the keyboard focus and it ends up in an infinite loop.
This also happens when opening a DAW project that carries more than one instance of the plugin. Ableton opens all plugin windows upon opening a project, although it keeps them non visible, they are being opened.
Tried different approaches to solve this but it seems the only way possible is to keep a variable in shared memory (Boost framework) or by using a messaging system.

Are you actually stopping the Timer when it first focuses itself? I’m not quite understanding how it would keep calling in an infinite loop if each instance stopped the Timer the first time it happened.