JUCEApplication doesn't receive keypresses

Hi all,

I want to build an audio app with multiple document windows. In fact I want the app being able to open without any DocumentWindow opened and then using keystrokes like command-N to open a new DocumentWindow, later on creating a (macOS) menubar with a “New …” command.
I’ve created a custom MainApplication class, inheriting from JUCEApplicationBase and ApplicationCommandTarget, copied almost all the code of JUCEApplication and edited the virtual functions of ApplicationCommandTarget like this:

void MainApplication::getAllCommands (Array<CommandID>& commands)
{
    commands.add (StandardApplicationCommandIDs::quit);
    commands.add(CommandIDs::newWindow);
}

void MainApplication::getCommandInfo (const CommandID commandID, ApplicationCommandInfo& result)
{
    
    switch (commandID) {
        case StandardApplicationCommandIDs::quit:
            result.setInfo (TRANS("Quit"),
                            TRANS("Quits the application"),
                            "Application", 0);
            result.defaultKeypresses.add (KeyPress ('q', ModifierKeys::commandModifier, 0));
            break;
        case CommandIDs::newWindow:
            result.setInfo("New Window", "opens another window", "Application", 0);
            result.defaultKeypresses.add(KeyPress('n', ModifierKeys::commandModifier, 0));
            break;
        default:
            break;
    }
}

bool MainApplication::perform (const InvocationInfo& info)
{
    
    switch (info.commandID) {
        case StandardApplicationCommandIDs::quit:
            systemRequestedQuit();
            DBG("QUIT");
            return true;
            break;
        case CommandIDs::newWindow:
            DBG("NEW WINDOW");
            createNewDocumentWindow();
            return true;
            break;
        default:
            return false;
            break;
    }
    
}

I suppose there is some kind of key listener missing for listening to the key presses when no DocumentWindow is opened. However, Quit (cmd-Q) is working globally (as it is in JUCEApplication).

Any ideas?

Thanks
Stefan

Isn’t an ApplicationCommandManager required?
Have a look on the menu demo?
Note that without a default component at start it makes things a bit harder than i thought.
That’s why i need more time to join an attempt/exemple! :laughing:

Yes, I have an ApplicationCommandManager in my MainClass and under void initialise (const juce::String& commandLine), I also called applicationCommandManager.registerAllCommandsForTarget(this);

I studied the Menus Demo PIP in the example folder, but everything is set up in a Component/DocumentWindow…

A quick (errors are mine) example that seems to work fine.
Experts please correct me if something is wrong!

Main.cpp (3.4 KB)
Foo.zip (2.5 KB)

AWESOME! Thank you so much Nicolas! That’s doing exactly what I need.
I have to look deeper into this, but it seems that when the MenuBarModel is missing, the keypress in the ApplicationCommandManager doesn’t work either. When both, MenuBarModel and ApplicationCommandManager are set up correctly, everything is working fine.

Thanks again!

Don‘t know if you covered that, but what I forgot at first was to add each window as keylistener:
https://docs.juce.com/master/classKeyPressMappingSet.html

Usually it is, but in that case it was about the contrary.
Key shortcuts without any window.
TDLR: it works fine out of the box with a properly set machinery.