- Home /
4.6 UI: How to invoke button click on Enter key
My mobile app starts with a simple login screen. Users should be able to:
Type in username and password
Tap Enter/Return on the on-screen keyboard to submit
They should not have to tap the actual button on the screen.
I've been poring over the documentation for EventSystem, Selectable, Button, InputField, etc. for hours now, and I still can't figure out how all this is supposed to work.
Obviously, I could just hard-code listening for the Enter key in some script and then invoke the action I want, but that seems like a hack when there's stuff like StandaloneInputModule.submitButton, ISubmitHandler, and Button.OnSubmit lying around.
This scenario is so easy to set up in most UI frameworks, I figure I must be missing something.
What's the right way to do this?
I have been poking around for this, please somebody guide us the right way on how to use the uGUI (the new EventSystem) equivalent of the old GUI OnButtonDown, OnButtonUp etc...
Answer by SirAstral · Jul 27, 2015 at 08:48 PM
If I understand you correctly then you are looking for the End Edit eventhandler for the password textbox. It is exposed in the inspector so all you need to do when editing is select the password textbox in the hierarchy, and scroll down in the inspector, then expand the input field (script) to see them.
You can hit the + key on End Edit, then you will need to add a script or object that will handle the logic for verifying the username password for example, and then you will need to set the "No Function" dropdown to the name of the method you want it to execute when done. Pay mind that the method on that object or script must be public or you will not be able to select the fuction that performs the login.
For anyone wondering, On Value Change executed the attached method every time you add or remove a letter from the textbox while you are typing. The End Edit is called when you leave the text box by clicking outside of it or enter and even tabbing if you have that wired up.
Answer by moriggi · Jul 28, 2015 at 04:38 AM
hello create the text field name the password and a button inside a canvas Unity 4.6 at least. add an event handler in the button, drag and drop the gameobject containing the script that run the logic from the hierarchy into the add event, select the function in the handler event of the button.