TextEntryComponent has no release focus action


While in text entry component, pressing escape key does not release the focus. From a quick look at TextEntryComponent source code, looks like it does not have a release focus action.

Curious if this is a missing feature or if there was a reason it has not included?

Lack of escape is probably an oversight but “tab” for sure will take you out of the field and “enter” can do so, too, I think.

1 Like

Yes, but I guess it won’t help with multi-line text fields, or when there are multiple text fields next to each other as it will move the focus to the next field.

Because a focused text field is blocking the key input, having a release-focus action will be very helpful when you want to quickly switch out of the text field to be able to listen to key inputs in other places like the camera state,… (useful in editors).

1 Like

I had something where I needed ENTER to do a certain action.
Just had a look to that code snippet. I found two ways for you - not sure if they are best, but for me they worked.

Way 1: Abuse ActionListener

this.getApplication().getInputManager().addMapping("Neue_Sprache", new KeyTrigger(KeyInput.KEY_RETURN));

and in the ActionListener I do like this:

private final ActionListener actionlisteneraddlng = new ActionListener() {
        public void onAction(String name, boolean isPressed, float tpf) {          
            if (name.equals("Neue_Sprache") && !isPressed) {
                if (GuiGlobals.getInstance().getFocusManagerState().getFocus() instanceof TextField) {
                    if (txtfieldnewlng == (TextField) GuiGlobals.getInstance().getFocusManagerState().getFocus()) {
                        GuiGlobals.getInstance().getFocusManagerState().setFocus(null);  ---> FOCUS LOST
                        if (addnewlng()) {  --> in my example I had a popup that will be removed now


Problem here is you might need to clean the Listener once you remove that AppState also you would need to make a list or another check that it only works in Textfields you want

Way 2: Not sure if it works, it is labeled ToDo

txtfieldnewlng.getActionMap().put(new KeyAction(KeyInput.KEY_RETURN), new KeyActionListener() {
          public void keyAction(TextEntryComponent arg0, KeyAction arg1) {
           // "On enter do the following, without need  to add additional mappings and no need to remove them";

If you find something that works - please share it here.

1 Like

Yes, this should work (but note that this will overwrite the existing action mapped to Enter key). I think this is the preferred way to add our own actions to the text field. The listener be enabled when the focus is gained and disabled when the focus is lost. I use this way to add “Paste clipboard action” (Ctrl-V) to text fields when needed.

It would be nice if we could also do this via the Lemur styling framework.

But in the case of release focus action mapped to Escape key, I rather implemented it inside the TextEntryComponent class instead of externally adding it in each and every place.

Hmmm… I thought action maps could be styled. I’ll have to look into that.

One potential issue with building escape support in by default is where to send the focus. It has to go somewhere or you will strand players that do not have a mouse cursor (joystick only interfaces, etc.).

“Next” is the only obvious choice but may not solve the “UI is trapping me” issue if the next field is also a text entry.

Another option is to move to the last item in the focus chain (assuming that can be detected.) At least in a dialog this is often the cancel button.

Hmmm… I do not have a joystick to test this but using a joystick one can not control the mouse cursor? This is bad news to me.

If you could give a feedback, I would be interessted.

The joystick definitely does not control the mouse cursor.

There are some types of games that never have a mouse cursor at all.

1 Like

I also do it that way (not having my computer with me) but its an external class that I add to each textField needed.

Feedback on what? The action map is definitely the “right way”. I didn’t really look in detail at the other way because it’s not the “right way”. Ali_rs already said this though so I didn’t think it needed to be said.

To control edit actions then you should use the action map because the action map is for controlling edit action. (Whole purpose for existing.) Just like Swing.

Was there something else I needed to provide feedback about?

You said you wanted to look if the actionmap can be done via styling.

Ah, the forum is very bad at unquoted threading. I only just realized I can click to make things sensible.

I thought that one of the motivations for creating the action map was so that it could be styled but I don’t know when I will have a chance to look at it.

I noticed there is a TraversalDirection.End in the FocusTraversal interface that should navigate to the last element.

1 Like