Keyboard focus


#1

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?


#2

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.


#3

A label does exactly what I want. Thanks.


#4

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?


#5

+1 for jazzunko relevant question :slight_smile:


#6

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).


#7

I think understood.
Thanks, Jules.


#8

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?


#9

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.


#10

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.


#11

Use a LookAndFeel…

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

Rail


#12

Thank you.

I missed it when looking at LookAndFeel.


#13