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.
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:
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 finally found the solution!
Thanks to this blog post: How to Add Your Own Tools to Unity’s Editor
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);
};
}
}