Using keys besides enter / escape to control UI textbox input?

New input system.

So normally you need to either press enter to confirm your textbox input or escape to cancel it. Is it possible to use different keys for that? I ask because I would like the gamepad ‘A’ button to do the same as enter, and the gamepad ‘B’ button to do the same as escape in this case.

Similarly I want gamepad ‘B’ button to cancel when we are in a dropdown menu.

Any advice appreciated. Thanks.

If you’re talking about Unity UI (Not IMGUI) then you can replace your Standalone Input Module (a component that must exist for UI events to process) with the Input System UI Input Module as described here. From there you can give it your Input Action Asset which has all your definitions and you can even import all the default UI actions into an Input Action Map (the categories within your asset) called UI. Then you’ll be able to edit and add whatever bindings you want to control the UI:

What you’re looking for is probably “Submit” and “Cancel”.

1 Like

Thanks for the reply. I haven’t been able to figure out how to import all the default UI actions in my Input Action Map.
If I click the ‘+’ next to Action Maps it just lets me create a new one but can’t seem to find where to import the default UI actions.

edit: actually I was able to import all the defaults now to my main input action asset. However it still doesn’t seem to be using the new input actions. Is ‘standalone input module’ a component that is supposed to exist on a gameobject? I can’t seem to find it. I added the ‘Input System UI Input Module’ and set it up already, but wondering if I need to delete the ‘standalone input module’ somewhere?

When you first add in UI to a scene I believe it adds a GameObject called “EventSystem” or something like that which has the standalone input module. So yeah I think you would need to delete that before it can start to use the input system module. If it’s not there maybe you can do a component search or check if you have actually added a canvas to the scene and not just loading it in at runtime.

1 Like

I am able to create extra mappings for Submit and Cancel now, thanks to your advice. Those extra mappings work for cancelling or submitting a dropdown menu selection, however they do not work for textfields. Like after you enter text in a textfield and you press ENTER/ESCAPE, I guess it’s not using the mappings for Submit/Cancel there and instead something else?

I would’ve thought Input Field would use Submit for entry but actually I could be mistaken. What I’ve always done the past few years is run whatever “User hit enter” code I have by just reading the value of the pressed key/running from callback set up with Input System. So actually I don’t think there is a concept in Unity for “Submitting” the input field text, only a Unity Event that gets called when you finish editing the text. You can read about that here. You might be right about this code utilizing only keyboard enter and not the “Submit” button since you are typing text, but keep in mind this event is also run when you simply deselect the field as well.

So just to recap: if you want your “Submit” button to have an effect on your program, you can just register a callback or use InputAction.ReadValue() like you would detecting any other button and have that run whatever code you need to run the task. If you want the Submit button to only take effect when the user is in “typing mode”, you can check to see if the Input Field is currently selected with EventSystem.current.currentSelectedGameObject (it will return the Input Field and you can verify the components available on it). The input field’s text can be read via code, and you might even be able to run the On End Edit event from code as well. So basically, you should have everything you need to get any sort of behavior you want for the button, just using traditional state tracking and calling the right references.

1 Like