x


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");
    }
}
more ▼

asked Jan 08 '11 at 12:37 AM

yoyo gravatar image

yoyo
6.4k 25 39 84

Feb 21 '12 at 07:14 PM Veehmot

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

Mar 03 '12 at 10:56 PM asafsitner
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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) && (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<string>("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<string>("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<int>("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. :)

more ▼

answered Feb 06 '11 at 10:21 AM

nathanp812 gravatar image

nathanp812
57 6 6 9

Thanks, I'll give that a try.

Feb 06 '11 at 08:26 PM yoyo
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3678
x1670
x150
x69
x36

asked: Jan 08 '11 at 12:37 AM

Seen: 6111 times

Last Updated: Mar 03 '12 at 10:56 PM