I need some conditions for submitting the text inside of an Inputfield, however I cannot find a way to do this via code. I was expecting something simple like with WPF or WinForms, but I cannot find any method I can use to actually submit an Inputfield via code.

How can I submit an Inputfield through code so that I can add some sort of conditions to when it can be submitted by the user other than just the enter button.


Edit, some clarification:

Submitting an input field is submitting it's text, firing an event which can be listened to by the OnSubmit method which then fires an onEndEdit event (I think event) which you can add listeners to so that specific methods of your own get called when it fires.

Right now I am unable to identify a reasonable way to change the keys or combination of keys that control when the form is submitted (Default ie "Enter" or "Return" in the Input Manager) (Can't use Shift-Enter for a new line since enter submits the form). Instead I want to manually submit the form through code when the appropriate keys are pressed.

public class ChatInputField : MonoBehaviour
    public InputField textInput;

    public void Start()
        textInput.onValueChange.AddListener(delegate { CheckForSubmit(); });

    //Checks for an enter to submit
    public void CheckForSubmit()
        //No idea how to get the actual value changed from this event so I have to check for keys being held down. Terribly awkward
        if (Input.GetKey(KeyCode.Return) || Input.GetKey(KeyCode.KeypadEnter))
            if(Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
               //Add a new line
                if(textInput.text != "")
                    //Submit form if not empty

Edit2: I just realized that I can manually edit the attributes I want. I can send it's text to the appropriate class, clear the input field, and de-select it if I wanted. No need to fire the event other than for ease-of-use within the inspector. Sorry for wasting you guy's time :)

I'll leave this question open in case someone does figure out how to fire these events manually though.

  • \$\begingroup\$ Can you give a use example here? Do you mean manually setting the text inside a UI InputField? What does "submit an Inputfield" mean? \$\endgroup\$
    – House
    May 8, 2015 at 22:51
  • \$\begingroup\$ Added some more information, let me know if you would like more. \$\endgroup\$ May 8, 2015 at 23:06
  • \$\begingroup\$ Added more information I just realized, I don;t even need to submit the event other than for ease of use within the inspector (using the UI's event system). I can just manually transfer the text to another class and clear the input field...etc \$\endgroup\$ May 8, 2015 at 23:10


You must log in to answer this question.

Browse other questions tagged .