- Home /
How to check if a key is down in editor script
I am writing an editor script and I want to be able to check if a key is or has been pressed regardless of what has focus. I realize that Input won't work. So far I haven't been able to find anything in the documentation or on Google that would allow me to do this. I was wondering how to do this or if it is even possible.
Answer by gabhead · Jul 28, 2011 at 03:11 PM
[CustomEditor(typeof(yourClassTarget))]
public class yourEditorClass: Editor
{
void OnSceneGUI()
{
VertexPaint script = (VertexPaint)target;
Event e = Event.current;
switch (e.type)
{
case EventType.keyDown:
{
if (Event.current.keyCode == (KeyCode.A))
{
script.Painting = true;
}
break;
}
}
}
}
This code should work, vote up everyone ! I used Event.current.type to check, too and it's working. But please think it twice before using the code like this, as this kind of code will get the input even user is typing the name of the GameObjects or rename / input some value and that may not what you want. Narrow down a bit by checking EditorWindow.focusedWindow should be good.
@thienhaflash: Yes, absolutely ;) A lot people don't think about the fact that the editor is ment for many different tasks. You could also, like the TerrainScript that comes with Unity, display your own "toolbar" and only check the key when your "editor" is active / in the right mode.
Answer by tribaleur · Apr 08, 2020 at 06:59 PM
I know that this post is archeology, but to complete the exemple code of the answer you have to add the instruction e.Use(); after your code.
[CustomEditor(typeof(yourClassTarget))]
public class yourEditorClass: Editor
{
void OnSceneGUI()
{
VertexPaint script = (VertexPaint)target;
Event e = Event.current;
switch (e.type)
{
case EventType.keyDown:
{
if (Event.current.keyCode == (KeyCode.A))
{
script.Painting = true;
// EDIT : this a the instruction to add.
// You realy need it to avoid performance issues !
e.Use();
// END EDIT
}
break;
}
}
}
}
No, not really. You don't generally need it and it doesn't really have much to do with performance. You should call Use when you specifically want to prevent any further processing by Unity or other scripts of that event. You really have to be careful with Use because you currently do not check any modifier keys. So your code will react whenever A is pressed in the sceneview. No matter is it's SHIFT+A, CTRL+A, ALT+A or just A
How do you actually get this code to run? I've put it in the Editor folder, but it never runs. Also, VertexPaint is unresolved.
HI @homer_3 , You can see on the first line that this is a CustomEditor
for yourClassTarget
. You should replace yourClassTarget
with the name of one of your own scripts, and by doing that, this Editor class will override the default display for your script: you will have to display the fields of your script yourself. More info on CustomEditors on the link below. Also, for your VertexPaint
issue, this is because VertexPaint
is a custom script that the owner has wrote, so lines 6 and 14 in this sample code are irrelevant for the resolution of this question. The owner should have removed them to avoid confusion. Hope this helps :)
[1] https://docs.unity3d.com/Manual/editor-CustomEditors.html
Your answer
Follow this Question
Related Questions
Prevent unity hotkeys in editor 0 Answers
SetReplacementShader in Editor Utility? 0 Answers
mouseposition and clicks in editor sceneview 0 Answers
breaking out of OnGUI() ticks? 1 Answer
Unity Editor Scripts: GameObject Added / Removed Event? 1 Answer