keyListening and routing for the whole application


So … I think I have an idea how to do this, but thought it would be good to see if anyone disagrees or has a better method.

I have a nice system for midilearn, where individual components register as a changeListener with the top level component - which is in turn registered with the midi managing component to receive all incoming midi messages. So incoming messages get disseminated via the high level window to all relevant UI elements as change messages. Pretty standard I guess.

I want to do the same for KeyPresses … so I register individual components, dynamically mapping various keys per user inputs to components … I use the convenient addShortcut() - which registers the UI elements as listeners with the top level window.

The problem is that in the first case (midi) I have just one midiManager that broadcasts all appropriate messages to the high level component … but in the case of keys, the KeyPressed() method is only executed on the currently focused component, and not necessarily passed on … so if my high level window doesn’t have the focus, the KeyPress is sometimes recieved and stamped out by the currently focused component.

I don’t want to constantly force the focus to the high level window, as this causes some issues …

So I am guessing that the way around this is to override the KeyPressed() method in custom classes, making sure the method returns false and passes the Key on up to the parent, which can then dispense a message.

Naturally, I don’t really want to do this if there is an easier way … and further, I am wondering about, say, modal dialogs … where I’m not sure I can override the KeyPressed method … ?

Anyway, thought I might have missed something simple here, so I figured I’d check before diving in.



Have you looked at ApplicationCommandManager and the related classes (ApplicationCommandTarget)? The juce demo uses this to dispatch keybard shortcuts so is well worth a look and is a lot easier than messing around with the keyPressed() methods of all your child components (believe me, I tried it this way too at first).


Aha ! Actually, I hadn’t realized that the keystrokes were being passed up to parent components pretty well most of the time (I was stopping them a couple levels from the top component, but since addShortcut() adds the call to the top level component, I just had to JUCE route it a little further).

Always the simple things.


There’s no global place that keypresses go - all OSes deliver them to individual windows, not to the app as a whole, and that’s a very sensible way to do it. It’s not a big deal to register your top-level windows with a global keypressmanager.