- Home /
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
Answer by steakpinball · Apr 02, 2015 at 06:26 PM
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.
O$$anonymous$$ -- I tried this, but having problems with Submit. Similar to this
http://forum.unity3d.com/threads/onsubmit-doesnt-fire-on-inputfield.273645/#post-1959106
I have to hit ENTER twice to get the event to fire.
Further searching reveals a lot of people struggling with EndEdit/Submit -- the only "solution" I've seen is to use EndEdit but to test for the ENTER key being down to avoid firing on focus change. That sounds hacky!
Answer by gresolio · May 27, 2017 at 09:56 PM
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);
}
}
You're welcome :)
Thanks for the kind words, now I feel better after not very good statements about this solution. Some people don't value the time of others at all.
Your answer
Follow this Question
Related Questions
WebGL build Japanese IME 0 Answers
Unity 4.6 InputFields need help 1 Answer
change GUI InputField to TMP_InputField 0 Answers
Terminal-like GUI, wait for input 1 Answer
Prevent to Deselect InputField 0 Answers