Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Frustrating problem with InputField and accepting Return and only Return to submit the field.

Discussion in 'UGUI & TextMesh Pro' started by bloomingdedalus, May 30, 2015.

  1. bloomingdedalus

    bloomingdedalus

    Joined:
    Aug 13, 2012
    Posts:
    139
    I'm pretty new to working with the 5.0 UI system. I'm trying to get an input field to process the line when I hit enter, but, strangely, I have to hit enter twice for it to accept the line. The first time I hit return, it just dumps focus of the input field. When I hit it a second time, it "flushes" the field.

    I've tried using the OnSubmit event, but I don't want it processing when the InputField loses focus. Unfortunately, enter seems to, by default, cause InputField to lose focus.

    Does anyone know how to get it to accept only one return to process the input field? This is the code I'm using in my update function at the moment:

    Code (csharp):
    1.  
    2.      // ConInIF = reference to InputField... everything else pretty much irrelevant.
    3.  
    4. void Update () {
    5.  
    6.      if(ConsoleEnabled)
    7.      {
    8.        if(Input.GetKey(KeyCode.Return) && ConInIF.text != "" && ConInIF.isFocused)
    9.        {
    10.          POConsoleR.Instance.WriteLine(ConInIF.text);
    11.          ConInIF.text = "";
    12.        }
    13.  
    14.        if(NewLine)
    15.        {
    16.          NewLine = false;
    17.  
    18.          ConOut.text = POConsoleR.Instance.FullFeed;
    19.  
    20.        }
    21.      }
    22.    }
     
  2. TechCor

    TechCor

    Joined:
    Apr 3, 2015
    Posts:
    56
    That behavior seems unusual. You say you've tried using the OnSubmit event, do you mean "End Edit"?

    Maybe you are trying to add the event via script.

    Here's what I do for input field processing with the addition of input field activation that might achieve what you are looking for.
    Code (csharp):
    1.  
    2. public void OnEditingEnded() // Set on InputField via inspector
    3. {
    4.   if (Input.GetKeyDown(KeyCode.Return) || Input.GetKeyDown(KeyCode.KeypadEnter))
    5.   {
    6.     // ... Perform Processing ...
    7.     m_InputField.ActivateInputField();
    8.   }
    9. }
    10.  
    If the ActivateInputField call doesn't work there for some reason (Unity is finicky sometimes). The usual workaround is to use a coroutine and wait until end of frame.
     
  3. bloomingdedalus

    bloomingdedalus

    Joined:
    Aug 13, 2012
    Posts:
    139
    I was reading that "EndEdit" also fires not if someone presses return, but also if the inputfield loses focus. The interface was somewhat annoying to try to figure out how to add by script (I'm sure I could do it if I sat down and read it, but I didn't feel up to it given that I was told it also fires when the field loses focus which isn't acceptable for my purpose - only Return can suffice as it's an inputfield for a console class).

    The problem I finally figured out is that the InputField automatically loses focus upon hitting return, so I stopped checking for focus as it wasn't particularly necessary for this class. It's more of a hack than a proper solution though as if you have multiple input fields and need return for each one, this won't work (just posting this in case someone else finds it and needs an answer):

    Code (csharp):
    1.  
    2.  
    3.        if(Input.GetKey(KeyCode.Return) && ConInIF.text != "" )
    4.        {
    5.          POConsoleR.Instance.WriteLine(ConInIF.text);
    6.          ConInIF.text = "";
    7.          ConInIF.ActivateInputField();
    8.        }
     
  4. TechCor

    TechCor

    Joined:
    Apr 3, 2015
    Posts:
    56
    Yes, because losing focus would be the end of editing, but that is why my code shows a conditional check for return inside of the End Edit. All you need to to do is reactivate the input field if you so wish.

    Take the code you just posted and stick it in EndEdit. Done.