Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity 5.3 - Input field wont detect first "return" key when pressed

Tags:

c#

unity3d

I have to double press "return" key for the value to be submitted! It was working fine like 2 hours ago(i only needed to press return once), but then i restarted Unity and now i need to double press "return" for the value to be submitted.

Update #2

I have a script attached to the canvas that is holding the Input Field. The code as follows :-

public class Example: MonoBehaviour {
    public InputField inputField; 

    void Start () {
    }

    void Update () {
    HandleUserInput ();
    }

    void HandleUserInput() 
    {

    if (inputField.isFocused && inputField.text != "" && (Input.GetKey (KeyCode.Return) || Input.GetKey(KeyCode.KeypadEnter))) {
        Debug.Log ("Pressed");
        //Do stuff
        inputField.text = ""; //Clear Inputfield text
        inputField.ActivateInputField(); //Re-focus on the input field
        inputField.Select ();//Re-focus on the input field
    }
    }

When i play the scene, and type in the field and press the first return, the Log wont show anything, but when i press it again it shows "Pressed".

Update #3

I found some kind of solution, if i remove "inputField.isFocused" The input field will detect the first "Return" key. But still, in my case, if the input field had some text in it then the user clicked anywhere else in the scene then pressed enter, the input field will submit it.

Please advice

like image 262
Abzax Avatar asked Mar 12 '23 10:03

Abzax


2 Answers

There is no need to do this manually. You need to use the Event System to register and receive events from InputField. Register to InputField submit event with InputField.onEndEdit, to get a callback when there is a submit on an InputField. You can also use inputField.onValueChanged to check when input changed. You can perform the null check in the callback functions:

public class Example: MonoBehaviour
{
    public InputField inputField;
    void Start()
    {
    }

    void Update()
    {
    }

    //Called when Input changes
    private void inputSubmitCallBack()
    {
        Debug.Log("Input Submitted");
        inputField.text = ""; //Clear Inputfield text
        inputField.ActivateInputField(); //Re-focus on the input field
        inputField.Select();//Re-focus on the input field
    }

    //Called when Input is submitted
    private void inputChangedCallBack()
    {
        Debug.Log("Input Changed");
    }

    void OnEnable()
    {
        //Register InputField Events
        inputField.onEndEdit.AddListener(delegate { inputSubmitCallBack(); });
        inputField.onValueChanged.AddListener(delegate { inputChangedCallBack(); });
    }

    void OnDisable()
    {
        //Un-Register InputField Events
        inputField.onEndEdit.RemoveAllListeners();
        inputField.onValueChanged.RemoveAllListeners();
    }
}
like image 106
Programmer Avatar answered Mar 20 '23 05:03

Programmer


Another solution to this problem is to check inputField.isFocused from the previous frame, because pressing the button makes inputField.isFocused turn to false.

like image 20
gopla29 Avatar answered Mar 20 '23 07:03

gopla29