# JUCEApplication doesn't receive keypresses

**URL:** <https://forum.juce.com/t/juceapplication-doesnt-receive-keypresses/45788>\
**Category:** General JUCE discussion\
**Created:** [May 1, 2021, 11:55am UTC](https://forum.juce.com/t/juceapplication-doesnt-receive-keypresses/45788 "2021-05-01T11:55:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![StefanS](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@StefanS](https://forum.juce.com/u/StefanS)\
**Post date:** [May 1, 2021, 11:55am UTC](https://forum.juce.com/t/juceapplication-doesnt-receive-keypresses/45788/1 "2021-05-01T11:55:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nicolasdanet](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/nicolasdanet/32/56_2.png) [@nicolasdanet](https://forum.juce.com/u/nicolasdanet)\
**Post date:** [May 1, 2021, 12:31pm UTC](https://forum.juce.com/t/juceapplication-doesnt-receive-keypresses/45788/2 "2021-05-01T12:31:55Z")

</div>

Isn’t an ApplicationCommandManager required?  
Have a look on the [menu demo](https://github.com/juce-framework/JUCE/blob/master/examples/GUI/MenusDemo.h)?  
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! 😆

---

<div class="post-metadata">

**Author:** ![StefanS](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@StefanS](https://forum.juce.com/u/StefanS)\
**Post date:** [May 1, 2021, 4:26pm UTC](https://forum.juce.com/t/juceapplication-doesnt-receive-keypresses/45788/3 "2021-05-01T16:26:29Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![nicolasdanet](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/nicolasdanet/32/56_2.png) [@nicolasdanet](https://forum.juce.com/u/nicolasdanet)\
**Post date:** [May 2, 2021, 6:58am UTC](https://forum.juce.com/t/juceapplication-doesnt-receive-keypresses/45788/4 "2021-05-02T06:58:07Z")

</div>

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

[Main.cpp](https://forum.juce.com/uploads/short-url/qBJi5aGWH2MoK6bFZ93gmZbd3el.cpp) (3.4 KB)  
[Foo.zip](https://forum.juce.com/uploads/short-url/u6xDIi9reBK1dhpt6N7mJChOTBT.zip) (2.5 KB)

---

<div class="post-metadata">

**Author:** ![StefanS](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@StefanS](https://forum.juce.com/u/StefanS)\
**Post date:** [May 2, 2021, 9:52am UTC](https://forum.juce.com/t/juceapplication-doesnt-receive-keypresses/45788/5 "2021-05-02T09:52:21Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [May 2, 2021, 11:02am UTC](https://forum.juce.com/t/juceapplication-doesnt-receive-keypresses/45788/6 "2021-05-02T11:02:17Z")

</div>

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](https://docs.juce.com/master/classKeyPressMappingSet.html)

---

<div class="post-metadata">

**Author:** ![nicolasdanet](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/nicolasdanet/32/56_2.png) [@nicolasdanet](https://forum.juce.com/u/nicolasdanet)\
**Post date:** [May 2, 2021, 12:58pm UTC](https://forum.juce.com/t/juceapplication-doesnt-receive-keypresses/45788/7 "2021-05-02T12:58:15Z")

</div>

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.
