Keyboard shortcuts for CommandItems not working

Hello everybody,

I currently have the issue that my CommandItems are working if I trigger them from the menu with the mouse, but nothing happens when I use the keyboard shortcut associated.

The problem occurs under Windows and with MacOS too.

I followed exactly the steps in the documentation of the ApplicationCommandManager, and basically they seem to work because the items are showing in the menu with their according shortcuts.

Does anyone have a suggestion what could cause that problem?

Does your command target have keyboard focus?

Yes it has.

I noticed also, that the menu is being focused for a short time if I press an assigned key combination.

Can you reproduce this behaviour with the KeyMappingsDemo example?

Yes. Same problem.

That’s odd. I can’t see this behaviour with the KeyMappingsDemo example or the Projucer (where we have a lot of keyboard shortcuts) on Windows or macOS. Are you using the latest JUCE? What OS are you using? Can you send a simple code snippet that reliably reproduces the behaviour?

I have the same problem on Windows 10 and MacOS 10.13.2

Finally I could get the demo to work and I also have no problems with the Projucer shortcuts.

Thus I guess I did something wrong with some listeners or focussing but I cannot spot it.

So here’s a sample showing exactly my problem:

You need to add ApplicationCommandManager's KeyPressMappingSet as a KeyListener to your top-level component so it can invoke commands when a key is pressed. Add this line to the constructor of your MainComponent:

addKeyListener (getKeyMappings());

As an aside, you shouldn’t inherit from ApplicationCommandManager and instead should have a global instance of one that you use. Take a look at how the Projucer does it, or look at the KeyMappingsDemo or MenusDemo examples.

Thank you very much, it is working now.

I missed that step as the docs make this point very clear.