Keyboard focus


I have a TextEditor, and I only want it to get get focus when it is clicked on, and I want it to lose focus when any other compnent is clicked on, even if the othe component doesn’t want the focus.

How can I to this?


hmm. Tricky. Would it be more appropriate just to use a Label and set it not to want keyboard focus? That’d then show the text editor when you click it but wouldn’t get focus any other way, and it’d always hide the editor when something else gets clicked.


A label does exactly what I want. Thanks.


Hi, Jules

I also have a similar problem. I can avoid it using a Label component instead of a TextEditor component, but I’d like to get a text at each input like textEditorTextChanged(). In this case how should I do?


+1 for jazzunko relevant question :slight_smile:


The Label class itself gets the text events from its editor, so you could make a subclass of Label to catch any events you need (remembering to pass the events up to the parent class too, of course).


I think understood.
Thanks, Jules.


I can not use a label, because I need multi line text input.

I could make it modal, but then it would not work on iOS or android, correct?

Is this what you suggest, because this works?

class CTextEditor : public TextEditor
    void mouseDown( const MouseEvent &e ) { if ( !isCurrentlyModal() ) enterModalState( true, nullptr, false ); }
    void inputAttemptWhenModal() { unfocusAllComponents (); exitModalState(0); }

I would suggest adding a flag ( exitOnExternalMouseClick ) to TextEditor that caused this to happen.

BTW, while we're talking about TextEditor, is there a way to set the blinking cursor color?


I guess it might just about work, but is a bit nasty. You should at least make it modal only when it gets the focus rather than on a mouse-click.


OK, got it.

Any news on how the change the caret color? I have a black background with white text for an input box. A black caret of course, will not show.


Use a LookAndFeel…

CaretComponent* YourLookAndFeelClass::createCaretComponent (Component* keyFocusOwner)
    CaretComponent* pCaret = new CaretComponent (keyFocusOwner);
    pCaret->setColour (CaretComponent::caretColourId, Colours::white);
    return pCaret;



Thank you.

I missed it when looking at LookAndFeel.