Keyboard shortcuts for CommandItems not working


#1

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?


#2

Does your command target have keyboard focus?


#3

Yes it has.

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


#4

Can you reproduce this behaviour with the KeyMappingsDemo example?


#5

Yes. Same problem.


#6

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?


#7

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.


#8

So here’s a sample showing exactly my problem:

https://drive.google.com/open?id=1xg4VvvRe_tAWJ63I7Omg-RSkq1T-MWaf


#9

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.


#10

Thank you very much, it is working now.

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