UI how to listen for OnSubmit

This is almost similar to

aInputField.onEndEdit.AddListener(delegate{whatever method});

However, I do not want to trigger the function on loose focus but only when the user presses enter

You can try this

  1. Add an Event Trigger component

  2. Add a new event type and select “Submit” from the list

  3. Hook in the corresponding function you wish to trigger

this works, but i have to hit enter twice. I haven’t tried on device

Based on the answer by @elenzil I wrote this code:

using System;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

[Serializable]
public class StringUnityEvent : UnityEvent<string>
{ }

[RequireComponent(typeof(InputField))]
public class InputFieldSubmit : MonoBehaviour
{
    public StringUnityEvent onSubmit;

    private InputField inputField;

    void Awake()
    {
        inputField = GetComponent<InputField>();
        inputField.lineType = InputField.LineType.MultiLineNewline;
    }

    void OnEnable()
    {
        inputField.onValidateInput += CheckForEnter;
    }

    void OnDisable()
    {
        inputField.onValidateInput -= CheckForEnter;
    }

    private char CheckForEnter(string text, int charIndex, char addedChar)
    {
        if (addedChar == '

’ && onSubmit != null)
{
onSubmit.Invoke(text);
return ‘\0’;
}
else
return addedChar;
}
}

Call the script “InputFieldSubmit.cs”. Then add it to a GameObject with an InputField component. It exposes a convenient “On Submit” event in the inspector.

Hi @aosome23 , @DaveCrowdStar ,
Instead of using OnSubmit(), directly use OnEndEdit(). OnEndEdit() works with single enter/return key. This is default UnityEvent that is invoked when the user finishes editing the text content either by submitting or by clicking somewhere that removes the focus from the Input Field. The event can send the current text content as a string type dynamic argument.
I know its quite a late reply and you must have already solved it. But hope this would help others .

another late answer, as of unity 5.3.3.

first, this is ridiculous, Unity. Submit is not the same as Lose Focus.

but here’s a workaround.
if you just want a single-line text field,
you can:

  1. set the text field to multi-line, newline.
  2. add an onValidateInput() callback.
  3. in your validation callback, if the character as an integer is 10,
    then set a flag “doSubmit” or something to true,
    and return character ‘\0’.
  4. in Update(), check if “doSubmit” is true, and handle accordingly.

Simple solution:

using UnityEngine;
using UnityEngine.UI;

public class CustomSubmit : MonoBehaviour
{
	public InputField Field;
	private bool wasFocused;

	private void Update()
	{
		if (wasFocused && Input.GetKeyDown(KeyCode.Return)) {
			Submit(Field.text);
		}

		wasFocused = Field.isFocused;
	}

	private void Submit(string text)
	{
		Debug.Log("Submit=" + text);
	}
}

listen to onEndEdit, and inside its listener check touchScreenKeyboard.status to differentiate between lost focus and ok button on soft keyboard tapped.