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
2
Question by thaiscorpion · Mar 11, 2012 at 03:26 AM · guiinputenter

How to call function when enter is pressed in text area?

Im trying to send information for a login system with two textfields, when i press login it sends the information perfectly but if I press enter it doesnt work. How can I detect this event with the enter key properly?

Ive got this so far:

 #pragma strict
 
 function Start () {
     
 }
 
 function Update () {
     if(Input.GetKeyDown("enter")){
         enterPressed = true;
     } else {
         enterPressed = false;
     }
 }
 
 var enterPressed : boolean = false;
 var Username : String = "";
 var Password : String = "";
 var loginSuc : boolean = false;
 var stringText : String = "";
 var loggedinUser : String = "Guest";
 
 
 function OnGUI () {
 
     GUI.Label (Rect (380, 380, 100, 750), "Username");
     Username = GUI.TextField (Rect (380, 400, 200, 25), Username, 25);
     
     GUI.Label (Rect (380, 425, 100, 750), "Password");
     Password = GUI.PasswordField(Rect(380,445,200,25), Password,"*"[0],25);  
     
     if (GUI.Button (Rect (445, 480, 70, 25), "Login") || enterPressed == true)
     {
          checkPassword();
          print ("Button pressed");
     }
     
 
     GUI.Label (Rect (440, 260, 120, 750), stringText);
     
     GUI.Label (Rect (800, 60, 90, 50), "You are logged in as "+ loggedinUser);
     
 }
 
 function checkPassword(){
     //set the WWWForm to send to the PHP page
     var sendLoginInfo : WWWForm = new WWWForm();
        sendLoginInfo.AddField("Username",Username);
        sendLoginInfo.AddField("Password",Password);
 
     //send the WWWForm via WWW
     var getdata : WWW = new WWW("login.php",sendLoginInfo);
     yield getdata; //Wait for the data to return
 
     //if TRUE >> Load level    
     if (getdata.text == "true") {
         login();
     } else {
         stringText = "Login Failed!";
     }
 }
 
 function login(){
     stringText = "Login Successful!";
     loggedinUser = Username;
 }
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

2 Replies

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

Answer by Kleptomaniac · Mar 11, 2012 at 03:46 AM

You use a GUI Event in OnGUI which determines whether the current event within the GUI focus is equal to the return keycode. So, in OnGUI (), you would do this:

 if (Event.current.keyCode == KeyCode.Return) {
 checkPassword();
 }

and if you wanted to specify it as a keydown event you would go:

 if (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Return) {
 checkPassword();
 }

Hope that helps! Klep

Comment
Add comment · Show 3 · 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 thaiscorpion · Mar 11, 2012 at 04:11 AM 0
Share

Wow thanks that was perfect I was getting really confuse with all the different ways to detect input but none were working :D works perfect now!

avatar image Kleptomaniac · Mar 11, 2012 at 04:13 AM 0
Share

No worries! Happy to help. :)

avatar image Bunny83 · Jun 20, 2012 at 03:30 AM 0
Share

Actually you should always check the event type, otherwise your code is executed for all keyboardevents that involve the return key.

avatar image
0

Answer by unimechanic · Jul 03, 2013 at 04:12 PM

This example should help:

http://forum.unity3d.com/threads/188202-Simple-dynamic-list-editor

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

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

6 People are following this question.

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

Related Questions

keyboard event "none" works as "enter" 0 Answers

gui list problem 2 Answers

GUI texture touch input problem 1 Answer

Updating GUI text with GetButtonDown? 1 Answer

How do I get a GUI Texture Button to act as a Input Key such as a T button on a key bored or so? 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