KeyPressed Signature



When I select to have the callback KeyPressed() inserted from the “Extra Callbacks to insert” menu in the Projucer, it uses:

bool component::keyPressed (const KeyPress& key)

Then, if I add a public KeyListener into the parent field of the Projucer setup, so that it inherits from KeyListener, I get the error "Allocating an object of abstract class type “myGuiClass” - “Unimplemented pure virtual method “keyPressed” in myGuiClass”.

So, it turns out that the method injected by selecting from the “Extra Callbacks to insert” menu isn’t the full signature needed, you actually need to override this:

virtual bool keyPressed (const KeyPress &key, Component *originatingComponent)=0

Am I doing something incorrectly? or should the projucer insert the correct method signature for this? When I unselect that option to have projucer automatically insert it, and insert the correct one and override it, my error goes away.


Maybe I am misunderstanding what you are trying to do, but Component simply has to implement the bool keyPressed(const KeyPress& key) method to be able to handle key presses. Why are you inheriting KeyListener?


I’m just trying to get JUCE to recognize keystrokes. The weird thing is, but simply placing that keyPressed() function in another app I was working on (with buttons, labels, text editors, etc, it automatically worked, but I put keyPressed() into my current GUI class (which has no buttons, just labels) and it wasn’t registering key clicks at all, so figured I needed to add a keylistener.

I’ll be the first to admit that I’m a bit confused on how listeners work in JUCE.


The Component needs its setWantsKeyboardFocus(bool wantsFocus ) called before it can get the keyboard focus and receive the key presses.

Perhaps if you need some kind of a set up where some component always needs to get every key press, inheriting KeyListener could be useful. (I haven’t myself used it so far.)


Awesome, setWantsKeyboardFocus(bool wantsFocus) did the trick. My biggest issue with the library is it being so big I don’t always know what to search for to fix a problem. I would’ve totally forgot about that method had you not mentioned it.

Thanks a ton.