class MainContentComponent : public AudioAppComponent, private KeyListener {
public:
MainContentComponent() {
setWantsKeyboardFocus(true);
addKeyListener(this);
//grabKeyboardFocus(); <-- seems to have no effect
}
bool keyPressed(const KeyPress &k, Component *c) override {
if( k.getTextCharacter() == 'x' ) ...
I think it would really help newcomers to have this code in a comment at the top of juce_KeyListener.h -- that setWantsKeyboardFocus really killed me. It is hiding in juce_Component.h which is a massive file!
It would be nice to know, for the sake of completeness, why juce_Component.h provides its own virtual keyPressed method.
ok, after testing, it turns out this is sufficient:
i.e. a component automatically implements a KeyListener.
So maybe this information could be given in juce_KeyListener.h... or do I just suck particularly hard at mining information? How would a smart person go about getting this knowledge?
I agree, its bitten me a few times myself, which is why it was still on the brain! Maybe a gentle assert in the library here or there? (adding a keypress listener without it being WantingKeypress?)
I just spent 30 minutes trying to figure this out, I am so glad I found this thread! I totally agree that the need to setWantsKeyboardFocus should be mentioned somewhere more visible.
Otherwise, a visible recommendation to all new developers to dig into the Demo project would be appreciated (since, according to the comments, it seems to be pretty relevant). I personally read all tutorials in the website, but afterwards started programming without going into the examples directory.