- Home /
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!
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.
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.
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 }
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.
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;
If it's deprecated, just use:
if (Event.current == Event.$$anonymous$$eyboardEvent('return')) {
enter = true;
}
It's the Input.eat$$anonymous$$eyPressOnTextFieldFocus that is deprecated.
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
Your answer
![](https://koobas.hobune.stream/wayback/20220613014007im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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