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.