- Home /
Listen for a key in edit mode
Hey,
How could I listen for a key in edit mode.
For example I want a specifid event to fire when I press the 'P' button, like a shortcut button.
I am gessing my script should have the [ExecuteInEditMode] tag but I could not find anything to listen for button events.
Would appriciate any help. Thanks in advance.
Answer by numberkruncher · Jan 16, 2013 at 04:26 PM
Yes there are a couple of ways that I know of doing this:
Method #1 - Custom Menu Items
Custom menu items can be created which can have their own keyboard shortcut. These are easy to create:
using UnityEngine;
using UnityEditor;
public static class MyMenuCommands {
[MenuItem("My Commands/First Command _p")]
static void FirstCommand() {
Debug.Log("You used the shortcut P");
}
[MenuItem("My Commands/Special Command %g")]
static void SpecialCommand() {
Debug.Log("You used the shortcut Cmd+G (Mac) Ctrl+G (Win)");
}
}
See:
http://docs.unity3d.com/Documentation/ScriptReference/MenuItem.html
Method #2 - Custom Editor / Scene GUI
When using a custom editor you can make use of keyboard shortcuts and mouse commands in scene view windows:
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(MySpecialMonoBehaviour))]
public class MyCustomEditor : Editor {
void OnSceneGUI() {
Event e = Event.current;
switch (e.type) {
case EventType.KeyDown:
break;
}
}
}
See:
I know its an old post, but this answer helped me today. I'm adding this in case anyone else encounters the same problem I had, which was trying to tell if the button was held down continuously.
It seems like OnSceneGui() was being called more frequently than keyboard events. So if you had a button held down, say to be used as a modifier for mouse input, you would get some calls to OnSceneGui() where there would be a keyboard event, then some OnSceneGui() where there would be no keyboard event. The docs seems to back this up: docs.unity3d.com/ScriptReference/EventType.$$anonymous$$eyDown.html "This event is sent repeatedly depending on the end user's keyboard repeat settings."
Solution is to keep the keyboard state yourself, toggling it on $$anonymous$$eyDown and $$anonymous$$eyUp events:
private void OnSceneGUI ()
{
Event e = Event.current;
if(EventType.$$anonymous$$eyDown == e.type && $$anonymous$$eyCode.RightControl == e.keyCode)
{
move$$anonymous$$ulti = true;
}
if(EventType.$$anonymous$$eyUp == e.type && $$anonymous$$eyCode.RightControl == e.keyCode)
{
move$$anonymous$$ulti = false;
}
this didn't work for me. what did work was this:
if(Event.current.modifiers == Event$$anonymous$$odifiers.Control)
{
Debug.Log("Control");
}
Thanks! well explained. And what about if I want to listen for space key in the first method ? Thanks again
Method 2 doesnt work for special keys because MacOS doesnt register KeyDown events in Unity for special keys.
Answer by KirillKuzyk · Apr 22, 2016 at 07:38 AM
I finally found the solution! Thanks to this blog post: http://code.tutsplus.com/tutorials/how-to-add-your-own-tools-to-unitys-editor--active-10047
using UnityEditor;
using UnityEngine;
[InitializeOnLoad]
public static class EditorHotkeysTracker
{
static EditorHotkeysTracker()
{
SceneView.onSceneGUIDelegate += view =>
{
var e = Event.current;
if (e != null && e.keyCode != KeyCode.None)
Debug.Log("Key pressed in editor: " + e.keyCode);
};
}
}
When using the "onSceneGUIDelegate" your should be really careful what you do and don't use too simple shortcuts. It's always better to provide a way to disable the shortcut which isn't possible the way you assign the delegate. At least a configuration EditorWindow would be great.
$$anonymous$$ost hotkey are either context specific (in which case method 2 or numberkruncher's answer applies) or they are global hotkeys (method1). Global hotkeys should always use at least one modifier key like (CTRL, ALT).
Also if you use any GUI event (either in onSceneGUIDelegate or in a custom editor's OnSceneGUI) you should call e.Use()
in order to consume the key to prevent other code to react to that keypress.
Your answer
![](https://koobas.hobune.stream/wayback/20220613091031im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why does my Unity Serialization not work? Do i miss something? 1 Answer
Multiple editor windows combined 0 Answers
Reflection on class variable values null on editor startup 0 Answers
Shortcut Key to Maximize Viewport 6 Answers
Editor Color Picker issues 0 Answers