- Home /
Steal keyboard event Alpha0-9 and disable Unity editor hotkey in custom editor script
I'm working on an editor script to draw inside of the sceneview. I'm stuck at the point, where I want to use the alpha numeric key (numbers above the letters) to switch between brushes.
void OnSceneGUI(SceneView sceneView)
{
Event e = Event.current;
if (e.type == EventType.KeyDown)
{
int inputNum;
if (int.TryParse(e.character.ToString(), out inputNum))
{
Debug.Log(inputNum);
e.Use();
}
}
int controlID = GUIUtility.GetControlID(GetHashCode(), FocusType.Passive);
if (e.type == EventType.layout)
HandleUtility.AddDefaultControl(controlID);
}
Adding the default control works fine to block mouse input from selecting objects in the scene view. However, currently when I press the alphanumeric '2' key, the editor still toggles 2D camera mode, although I'm trying to use the event before Unity does. How can I steal that exact key before Unity uses it? Mouse command stealing works fine, and all other keys do not collide with anything, but I would still like to use the number keys.
Answer by Xarbrough · Jul 15, 2016 at 06:40 PM
I fixed my issue, but I don't understand why this makes a difference:
if (e.type == EventType.KeyDown)
{
int inputNum;
if (int.TryParse(e.character.ToString(), out inputNum))
{
Debug.Log(inputNum);
}
e.Use();
}
When I put the e.Use() call outside of the parse method it works. This is actually not entirely want I want, since I only want to steal the event, if it can parse the input correctly and it's a number. Now it just eats all key down events, but well at least it doesn't toggle 2D mode anymore.