Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
6
Question by yoyo · Jan 08, 2011 at 12:37 AM · guieditoreditorguitextfieldeditorguilayout

How can I determine when user presses enter in an editor text field?

My custom editor lets the user type in a text field ...

private string mString = "";
public override void OnInspectorGUI()
{
    mString = EditorGUILayout.TextField(mString);
}

How can I tell if the user pressed enter on the string?

This detects when the user presses enter ...

private string mString = "";
public override void OnInspectorGUI()
{
    mString = EditorGUILayout.TextField(mString);
    if ((Event.current.type == EventType.KeyUp) && (Event.current.keyCode == KeyCode.Return))
    {
        Debug.Log("pressed enter");
    }
}

But this detects enter when any control (or no control) in the editor has focus, not just my text field.

This worked in Unity 2.6.1 ...

private string mString = "";
public override void OnInspectorGUI()
{
    GUI.SetNextControlName("MyTextField");
    mString = EditorGUILayout.TextField(mString);
    if ((Event.current.type == EventType.KeyUp) && (Event.current.keyCode == KeyCode.Return) && (GUI.GetNameOfFocusedControl() == "MyTextField"))
    {
        Debug.Log("pressed enter");
    }
}

... but it doesn't in Unity 3.1. Seems like the text field has lost focus by the time the keyboard event comes through. (KeyDown doesn't work either.)

Here is the workaround I've come up with, but it seems awfully convoluted for what should be a simple thing. Something cleaner would be appreciated.

private string mString = "";
private bool mEditingMyString = false;
public override void OnInspectorGUI()
{
    GUI.SetNextControlName("MyTextField");
    mString = EditorGUILayout.TextField(mString);
    if (Event.current.type == EventType.KeyUp)
    {
        if ((Event.current.keyCode == KeyCode.Return) && mEditingMyString)
        {
            Debug.Log("pressed enter");
        }
        mEditingMyString = (GUI.GetNameOfFocusedControl() == "MyTextField");
    }
}
Comment
Add comment · Show 2
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 Veehmot · Feb 21, 2012 at 07:14 PM 0
Share

Even the example in the documentation is wrong: http://unity3d.com/support/documentation/ScriptReference/GUI.SetNextControlName.html

avatar image asafsitner · Mar 03, 2012 at 10:56 PM 0
Share

Not sure if still relevant, but the check for the event should come before rendering the text field or it will eat the event.

3 Replies

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

Answer by nathanp812 · Feb 06, 2011 at 10:21 AM

Well I am not sure whether you still require an alternative solution to your code above but this is how I do it.

Because Unity 3.x ships with the newer version of Mono, we have a new tool at our disposal called the extension method.

In a static class you create a generic extension method that evaluates the input like so.

public static bool KeyPressed<T>(this T s, string controlName,KeyCode key, out T fieldValue) {

 fieldValue = s;
 if(GUI.GetNameOfFocusedControl()==controlName)
 {
     if ((Event.current.type == EventType.KeyUp) &amp;&amp; (Event.current.keyCode == key))
         return true;
     return false;
 }
 else
 {
     return false;
 }

}

Then in the OnInspectorGUI() method you do the following for each field that you want to evaluate (changing the target class name to yours where required):

public override void OnInspectorGUI () { base.OnInspectorGUI ();

 GUI.SetNextControlName("Text1");
 if(EditorGUILayout.TextArea(((SimpleE)target).Text1).KeyPressed&lt;string&gt;("Text1",KeyCode.Return,out ((SimpleE)target).Text1))
 {
     Debug.Log("Enter key pressed in field Text1");
 }
 GUI.SetNextControlName("Text2");
 if(EditorGUILayout.TextArea(((SimpleE)target).Text2).KeyPressed&lt;string&gt;("Text2",KeyCode.Return,out ((SimpleE)target).Text2))
 {
     Debug.Log("Enter key pressed in field Text2");
 }
 GUI.SetNextControlName("Number1");
 if(EditorGUILayout.IntField(((SimpleE)target).Number1).KeyPressed&lt;int&gt;("Number1",KeyCode.Alpha1,out ((SimpleE)target).Number1))
 {
     Debug.Log("1 key pressed in field Number1");
 }

}

The reason I like doing it this way is that all the logic needed to test for any type of key press is located in the one location and can be reused anywhere within your project and I think it reads a little nicer.

It should be able to be used for any type of field type however I have only used it for int and string types.

Anyhow I hope it helps. :)

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 yoyo · Feb 06, 2011 at 08:26 PM 0
Share

Thanks, I'll give that a try.

avatar image
1

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

This example might help too:

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
1

Answer by mptp · Jan 05 at 11:04 AM

Just doing my part should anyone end up here from Google:

Check out DelayedIntField. There are similar functions for float, double, int and string, for both EditorGUI and EditorGUILayout.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

EditorGUILayout.TextField returning empty string 3 Answers

Fix editor TextField cursor alignment 1 Answer

Drawing Editor Inspector GUI based on selected/current prefab (CustomPropertyDrawer) 2 Answers

How do I prevent "Argument Exception: Getting control 1's position in a group with only 1 controls when doing repaint" in OnGUI function of my CustomPropertyDrawer? 2 Answers

Recreate default editor look manually without base.OnInspectorGUI [UnityEditor] 2 Answers


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