- Home /
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");
}
}
Even the example in the documentation is wrong: http://unity3d.com/support/documentation/ScriptReference/GUI.SetNextControlName.html
Not sure if still relevant, but the check for the event should come before rendering the text field or it will eat the event.
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) && (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. :)
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
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
.