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 JasonM · Oct 19, 2011 at 03:12 PM · guikeytextfieldreturnenter

Check if enter is pressed inside a GUI textfield

Hi everyone,

I'm trying to find a way to check if the user presses 'enter' or 'return' after entering their player name (rather than clicking the 'next' button). I found this code but it wont work when the textfield is in focus.

if (Event.current.Equals (Event.KeyboardEvent ("return"))) enter = true;

There has to be a way to check for key or input events when the user is focused on a textfield. Please help!

Comment
Add comment · Show 1
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 BerggreenDK · Oct 20, 2011 at 08:47 AM 0
Share

Welcome to this forum, but please read the FAQ on how to use Question and Answers.

Each question can contain many answers if many have an idea, but its not a community discussion forum. Use the normal Unity forum for that. QA is for quick answers to a question, thats why we can vote on the quality of the answer. An answer is NOT a comment.

6 Replies

· Add your reply
  • Sort: 
avatar image
8

Answer by TSorbera · Oct 19, 2011 at 07:05 PM

If it's not already, try putting your if (Event.current.Equals (Event.KeyboardEvent ("return"))) enter = true; line before your GUI call to create the text field. I had to do something similar when a button was "eating" a mouse press.

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
0

Answer by alkor · Oct 19, 2011 at 04:30 PM

Hi. I am a beginner in Unity, but I want to increase my knowledge, and I tried to find solution to this problem. Look at code below(it's C# and I added name space System(for work of Char))

void OnGUI() { s = GUI.TextArea(new Rect(30, 30, 100, 30),s,25); if((s != null)&&(s.Length != 0)) if (Char.IsControl(s[s.Length - 1])) //make this your action }

Comment
Add comment · Show 1 · 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 JasonM · Oct 19, 2011 at 05:50 PM 0
Share

Hi, glad to see your reply. I looked at your code but its hard to understand without formatting. Try clicking on the little button with 101010 in it to insert code snippet. I'm posting a solution I found in the mean time.

avatar image
0

Answer by JasonM · Oct 19, 2011 at 05:50 PM

Here's what I've come up with, there's got to be a better way, and this is really messy to me.

(inside the OnGUI put this so that itll read it when a textfield is active)

 Input.eatKeyPressOnTextFieldFocus = false;

Then inside the Window function put this - it actually does nothing in the OnGUI)

if (Event.current.Equals (Event.KeyboardEvent ("return"))) enter = true;

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 Waz · Jan 22, 2012 at 06:37 AM 0
Share

Unfortunately, this is now deprecated in 3.5.

avatar image -chris · Aug 17, 2012 at 07:25 AM 0
Share

If it's deprecated, just use:

 if (Event.current == Event.$$anonymous$$eyboardEvent('return')) {
     enter = true;
 }
avatar image Waz · Aug 17, 2012 at 09:34 AM 0
Share

It's the Input.eat$$anonymous$$eyPressOnTextFieldFocus that is deprecated.

avatar image
0

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

This example might also 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
avatar image
0

Answer by aanimation · Sep 02, 2014 at 10:07 AM

just get a ref : http://docs.unity3d.com/ScriptReference/GUI.GetNameOfFocusedControl.html

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

10 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

Related Questions

TextField plus Return Key issue 2 Answers

NGUI Input: Stopping Key Propagation 0 Answers

Backspace in limited length textfield causes ArgumentOutOfRangeException 1 Answer

GUILayout Textfield not editable just static 1 Answer

Can I change the keycode that clicks the focused Unity GUI button? 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