KeyListener functions?

Hi, I’ve added a public KeyListener to my components, to get it to compile I had to add four functions:
The first two are never called…

	bool keyPressed(const KeyPress& key, Component* originatingComponent) override
	{
		ignoreUnused(originatingComponent);
		DBG(String(key.getKeyCode())+", "+key.getTextCharacter());
		return true;
	}
	bool keyStateChanged(bool isKeyDown, Component* originatingComponent) override
	{
		ignoreUnused(isKeyDown, originatingComponent);
		return true;
	}

	bool keyPressed(const KeyPress& key) override
	{
		DBG(String(key.getKeyCode()) + ", " + key.getTextCharacter());
		return true;
	}
	bool keyStateChanged(bool) override
	{
		return true;
	}

If I delete the first two I get this error:

1>..\..\Source/Panel.h(295,7): warning : 'Panel::keyPressed' hides overloaded virtual function [-Woverloaded-virtual]
1>D:\Plug-ins\JUCE\modules\juce_gui_basics/keyboard/juce_KeyListener.h(66,18): note: hidden overloaded virtual function 'juce::KeyListener::keyPressed' declared here: different number of parameters (2 vs 1)
1>..\..\Source/Panel.h(300,7): warning : 'Panel::keyStateChanged' hides overloaded virtual function [-Woverloaded-virtual]
ins\JUCE\modules\juce_gui_basics/keyboard/juce_KeyListener.h(83,18): note: hidden overloaded virtual function 'juce::KeyListener::keyStateChanged' declared here: different number of parameters (2 vs 1)

And If I delete the second two I get this error:

…..\Source/Panel.h(282,7): warning : ‘Panel::keyPressed’ hides overloaded virtual function [-Woverloaded-virtual]
1>D:\Plug-ins\JUCE\modules\juce_gui_basics/components/juce_Component.h(1908,18): note: hidden overloaded virtual function ‘juce::Component::keyPressed’ declared here: different number of parameters (1 vs 2)
1>…..\Source/Panel.h(289,7): warning : ‘Panel::keyStateChanged’ hides overloaded virtual function [-Woverloaded-virtual]
1>D:\Plug-ins\JUCE\modules\juce_gui_basics/components/juce_Component.h(1932,18): note: hidden overloaded virtual function ‘juce::Component::keyStateChanged’ declared here: different number of parameters (1 vs 2)

Note the (2 vs 1) and (1 vs 2)
Can someone help, what is going on here? Why do I need to override four functions, when only two are ever called?

I apologise if it’s easy, but I don’t understand why.
I’m using the latest Juce master 8.0.3
Dave H.

Your Panel class inherits void keyPressed(const KeyPress&) from Component and void keyPressed(const KeyPress&, Component*) from KeyListener.
When only one of both is present, the compiler gives a heads up, that you might have messed up the correct number of arguments in your overload. When both are there, the warning goes away, because the compiler thinks you know what you are doing.

See also a similar question at StackOverflow

1 Like

Wow thanks for the quick reply, Daniel. Perfect. I clearly have no idea what I’m doing today, a lot on my plate :roll_eyes:.
So I don’t need to add the KeyListener class at all? It’s just for non-component classes. The second parameter makes sense now.

Yes, or to be precise for any class that wants to listen to a component for key presses.
The key events are a bit different from the mouse events. The Component is a MouseListener by default, but the KeyPresses are dispatched directly to the component with the KeyboardFocus (and only that one).