- Home /
Submit inputField when Enter is clicked
I tried the End Edit but it also submits when I click outside the inputField and I don't want that.
I also tried the Event Trigger with type Submit but it doesn't submit.
How can I submit an inputField only when the user clickes enter?
Edit: I want to use Unity 4.6 UI
Answer by RicardoRibeiro · Dec 08, 2014 at 09:04 PM
void Update()
{
if (Input.GetKeyUp(KeyCode.Return)) { TapYourSubmitFunction(); }
}
Answer by BlackDragonBE · Dec 18, 2014 at 07:12 PM
Don't put that in OnGUI(), put it in Update() instead.
OnGUI is used for the old GUI system.
I tried to place it in Update and it stopped to work. InputField.isFocused become false when Input.Get$$anonymous$$ey($$anonymous$$eyCode.Return) or Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Return) is true. OnGUI solution works well. I guess, event system uses OnGUI to update user input ( http://docs.unity3d.com/ScriptReference/Event.html ), so InputField looses focus before Update, but OnGUI tracks it.
Answer by ToneDP · Jun 27, 2016 at 01:44 PM
This still required me to hit enter twice to get it to fire.
I fixed it in this manner, by judging focus on the PREVIOUS update:
bool allowEnter;
void Update () {
if (allowEnter && (usernameInput.text.Length > 0) && (Input.GetKey (KeyCode.Return) || Input.GetKey(KeyCode.KeypadEnter))) {
OnSubmit ();
allowEnter = false;
} else
allowEnter = usernameInput.isFocused || passwordInput.isFocused;
}
It worked for me . Also instead of putting code in update i putted in fixedupdate now i don't need to click twice
Answer by alfish · Jan 19, 2017 at 09:32 AM
You can attach this to the InputField (change doSomething(...)
to your code for submitting).
It also allows a Button to call validateAndSubmit()
from onClick.
using UnityEngine;
using UnityEngine.UI;
/// <summary>Submits an InputField with the specified button.</summary>
//Prevents MonoBehaviour of same type (or subtype) to be added more than once to a GameObject.
[DisallowMultipleComponent]
//This automatically adds required components as dependencies.
[RequireComponent(typeof(InputField))]
public class SubmitWithButton : MonoBehaviour
{
public string submitKey = "Submit";
public bool trimWhitespace = true;
//Start is called on the frame when a script is enabled just before any of the Update methods is called the first time.
//Apropriate when initializing fields.
void Start() {
_inputField = GetComponent<InputField>();
_inputField.onEndEdit.AddListener(fieldValue => {
if (trimWhitespace)
_inputField.text = fieldValue = fieldValue.Trim();
if (Input.GetButton(submitKey))
validateAndSubmit(fieldValue);
});
}
InputField _inputField;
bool isInvalid(string fieldValue) {
// change to the validation you want
return string.IsNullOrEmpty(fieldValue);
}
void validateAndSubmit(string fieldValue) {
if (isInvalid(fieldValue))
return;
// change to whatever you want to run when user submits
doSomething(_inputField); // or doSomething(fieldValue);
}
// to be called from a submit button onClick event
public void validateAndSubmit() {
validateAndSubmit(_inputField.text);
}
}
Answer by Andergraw · Nov 29, 2017 at 06:00 PM
Yeah, but it's kind of useless, since it hits the button whether you send "Return" or focus out...