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?
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.
Edit:
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).
Hi,
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));
this.getApplication().getInputManager().addListener(actionlisteneraddlng,"Neue_Sprache");
and in the ActionListener I do like this:
private final ActionListener actionlisteneraddlng = new ActionListener() {
@Override
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
getState(PopupState.class).closePopup(poplngadd);
}
}
}
}
}
};
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() {
@Override
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.
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.
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.
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?
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.