Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
8
Question by RicardoRibeiro · Dec 08, 2014 at 06:05 PM · inputenterunity4.6submit

Submit inputField when Enter is clicked

I tried the End Edit but it also submits when I click outside the inputField and I don't want that.

I also tried the Event Trigger with type Submit but it doesn't submit.

How can I submit an inputField only when the user clickes enter?

Edit: I want to use Unity 4.6 UI

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

10 Replies

· Add your reply
  • Sort: 
avatar image
14
Best Answer

Answer by RicardoRibeiro · Dec 08, 2014 at 09:04 PM

 void Update()
     {
         if (Input.GetKeyUp(KeyCode.Return)) { TapYourSubmitFunction(); }
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
11

Answer by BlackDragonBE · Dec 18, 2014 at 07:12 PM

Don't put that in OnGUI(), put it in Update() instead.
OnGUI is used for the old GUI system.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RicardoRibeiro · Dec 18, 2014 at 08:40 PM 0
Share

I will change it, thank you very much

avatar image GuidC0DE · Feb 16, 2016 at 02:16 PM 5
Share

I tried to place it in Update and it stopped to work. InputField.isFocused become false when Input.Get$$anonymous$$ey($$anonymous$$eyCode.Return) or Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Return) is true. OnGUI solution works well. I guess, event system uses OnGUI to update user input ( http://docs.unity3d.com/ScriptReference/Event.html ), so InputField looses focus before Update, but OnGUI tracks it.

avatar image
6

Answer by ToneDP · Jun 27, 2016 at 01:44 PM

This still required me to hit enter twice to get it to fire.

I fixed it in this manner, by judging focus on the PREVIOUS update:

         bool allowEnter;
         void Update () {
             
             if (allowEnter && (usernameInput.text.Length > 0) && (Input.GetKey (KeyCode.Return) || Input.GetKey(KeyCode.KeypadEnter))) {
                 OnSubmit ();
                 allowEnter = false;
             } else
                 allowEnter = usernameInput.isFocused || passwordInput.isFocused;
         }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kaweri · Oct 16, 2020 at 12:09 PM 0
Share

its very helpful for me,many thanks!

avatar image droid85 · Mar 24, 2021 at 02:37 PM 0
Share

It worked for me . Also instead of putting code in update i putted in fixedupdate now i don't need to click twice

avatar image
2

Answer by alfish · Jan 19, 2017 at 09:32 AM

You can attach this to the InputField (change doSomething(...) to your code for submitting).
It also allows a Button to call validateAndSubmit() from onClick.

 using UnityEngine;
 using UnityEngine.UI;
 
 /// <summary>Submits an InputField with the specified button.</summary>
 //Prevents MonoBehaviour of same type (or subtype) to be added more than once to a GameObject.
 [DisallowMultipleComponent]
 //This automatically adds required components as dependencies.
 [RequireComponent(typeof(InputField))]
 public class SubmitWithButton : MonoBehaviour
 {
     public string submitKey = "Submit";
     public bool trimWhitespace = true;
     //Start is called on the frame when a script is enabled just before any of the Update methods is called the first time.
     //Apropriate when initializing fields.
     void Start() {
         _inputField = GetComponent<InputField>();
         _inputField.onEndEdit.AddListener(fieldValue => {
             if (trimWhitespace)
                 _inputField.text = fieldValue = fieldValue.Trim();
             if (Input.GetButton(submitKey))
                 validateAndSubmit(fieldValue);
         });
     }
     InputField _inputField;
 
     bool isInvalid(string fieldValue) {
         // change to the validation you want
         return string.IsNullOrEmpty(fieldValue);
     }
     void validateAndSubmit(string fieldValue) {
         if (isInvalid(fieldValue))
             return;
         // change to whatever you want to run when user submits
         doSomething(_inputField); // or doSomething(fieldValue);
     }
     // to be called from a submit button onClick event
     public void validateAndSubmit() {
         validateAndSubmit(_inputField.text);
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by Andergraw · Nov 29, 2017 at 06:00 PM

Yeah, but it's kind of useless, since it hits the button whether you send "Return" or focus out...

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

IPointerEnterHandler / IPointerExitHandler and touch 1 Answer

Enter not being called 2 Answers

Enter and Return in TextField in Mac Not Working 1 Answer

Enter key no longer sends message when I change text styles 1 Answer

How could I combine these two scripts 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges