How to prevent Input Field firing End Edit on change of focus?

I have an input field that is using End Edit to fire off some logic. End Edit is triggered when the user pushes a real (or virtual, on mobile) Enter key – which is as desired. However, if the player clicks in the input field and then clicks outside it (changing focus?) then End Edit fires also.

Is it possible to prevent it from firing when this happens – or do detect this has happened, so I can ignore it?

–Sam

It sounds like you should be using the OnSubmit event from the event system instead of EndEdit from the input field. Use it By adding an EventTrigger component to a game object and configuring your logic for a Submit event. Beware the submit event is fired on your object regardless of what currently has focus.

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);
	}
}