- Home /
Detect Enter Key Event with GUILayout.TextField Focused
Hey guys,
Working on an editor script that will allow my designers to modify NPC Waypoints easily. Right now I want to allow them to change the name of each waypoint for the sake of clarity.
Currently, if you press the GUILayout.Button to toggle the edit for each waypoint on and off, it works fine. I also wanted to give the option to simply press Enter. This "works" but only when the sceneview has been selected. So currently, you have to click the "change" button, type the name you want, select the scene view, then press enter.
Is there a way I can get the enter event to be detected from within the focus of the EditorWindow itself (or more specifically the TextField) without having to select the Scene View each time?
Here is a simplified version of my code with only the applicable code included.
Any suggestions are appreciated! Thanks guys!
void SceneGUI(SceneView sceneView)
{
//If we are changing the name of ANY of the waypoints, this should be true
if (changingName)
{
Debug.Log("Changing");
//Check for Enter Key Press
if (Event.current.keyCode == KeyCode.Return)
{
Debug.Log("hit");
waypoints[nameChangeID].GetComponent<Waypoint>().changingName = false;
SceneView.RepaintAll();
Repaint();
changingName = false;
}
}
}
void OnGUI()
{
//For each waypoint, draw a row in our inspector
for (int i = 0; i < waypoints.Count; i++)
{
GameObject go = waypoints[i];
GUILayout.BeginHorizontal();
//Make sure we don't print anything that might have turned up null
if (go != null)
{
//Print a Label or TextField based on the changingName state of each instance of a Waypoint
if (go.GetComponent<Waypoint>().changingName)
go.GetComponent<Waypoint>().name = GUILayout.TextField(go.GetComponent<Waypoint>().name, 20);
else
GUILayout.Label(go.GetComponent<Waypoint>().name);
//If the "Change Name" button is clicked, toggle changeName bool
if (GUILayout.Button(Resources.LoadAssetAtPath<Texture>("Assets/Scripts/AI/Patrolling Editor/Images/pencil.png"), GUILayout.Width(30), GUILayout.Height(30)))
{
Waypoint wps = go.GetComponent<Waypoint>();
//If true, set false, confirm name
if (wps.changingName)
{
wps.changingName = false;
changingName = false;
SceneView.RepaintAll();
}
else //If false, set true, set the ID of the item being changed so we can confirm it with the enter button
{
wps.changingName = true;
changingName = true;
nameChangeID = i;
SceneView.RepaintAll();
}
}
}
}
}
Answer by BenKurdziel · Oct 02, 2014 at 12:56 AM
Solved my own problem. The issue seems to be that anything inside SceneGUI is dedicated to the Scene View and cannot be called unless the SceneView is currently focused.
To resolve this, I simply put the code for the enter key event into OnGUI (which is registered by the inspector instead), and it worked like a charm.
void OnGUI()
{
if (changingName)
{
if (Event.current.keyCode == KeyCode.Return)
{
waypoints[nameChangeID].GetComponent<Waypoint>().changingName = false;
SceneView.RepaintAll();
Repaint();
changingName = false;
GUIUtility.hotControl = 0;
Event.current.Use();
}
}
Hope this helps if anyone has a similar problem!
Your answer
Follow this Question
Related Questions
Listen to Keyboard Events on EditorWindow 1 Answer
How do I wait until after AssetDatabase creates an assets before carrying out another function ? 0 Answers
Associate my custom asset with a custom EditorWindow 3 Answers
Any way to attach a bit of code to individual UI Windows [Editor] 1 Answer
How to "paint" objects in the editor? 3 Answers