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

拥有回忆 提交于 2019-12-07 08:10:41

问题


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


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/38908834/unity-5-3-input-field-wont-detect-first-return-key-when-pressed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!