A bit confused by the keyboard focus thingy


Hi all,

I have a main component, with a main menu and some keyboard shortcuts associated to it (like ctr-c for copy, ctrl-z for undo, and so on …)
I noticed that they work only until I click on another component, then, by default the other component takes the focus, because in internalMouseDown, there’s this code

[code] if (! flags.dontFocusOnMouseClickFlag)
grabFocusInternal (focusChangedByMouseClick, true);

    if (checker.shouldBailOut())


I would like my main menu shortcuts to work, whatever component has the keyboard focus. Is there a way to do that ?

The other option would be to set the ‘dontFocusOnMouseClickFlag’ to true for every other component but that’s tedious and I guess that would prevent text entry on some text fields …


Hi dinaiz,
Well yes you have that facility in JUCE… You just need to implement the class ApplicationCommandTarget in your main menu… You will need to implement its 4 pure virtual methods and then call the handleCommandMessage of whatever component you want to perform the action in… A small example implementation for refresh = F5:

ApplicationCommandTarget* getNextCommandTarget()
    return findFirstTargetParentComponent();

void getAllCommands (juce::Array <CommandID>& commands)
    const CommandID ids[] = { Commands::Refresh};
    commands.addArray (ids, numElementsInArray (ids));

void getCommandInfo (CommandID commandID, ApplicationCommandInfo& result)
    case Commands::Refresh:
            result.setInfo ("Refresh", "Refresh everything", "General", 0);
            result.addDefaultKeypress (KeyPress::F5Key, 0);

bool perform (const InvocationInfo& info)
    if (info.commandID == Commands::Refresh)

This is an old version code but I don’t think you have a problem in figuring it out… I hope you got it then… Keep going…


Thank you very much ! I totally forgot to say I solved the problem. I did what you say in your post, however I had another keyboard listener in the application who returned always true, so the keypresses where never reaching the ApplicationCommandTarget. Now it’s solved. Thanks for your kindness !