Skip to content

Instantly share code, notes, and snippets.

@founderio
Last active May 27, 2018 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save founderio/716c0af4fe0dbc022cde55b68c240bdf to your computer and use it in GitHub Desktop.
Save founderio/716c0af4fe0dbc022cde55b68c240bdf to your computer and use it in GitHub Desktop.
Unity Editor global hotkeys as of 2018.1
[InitializeOnLoad]
public static class StaticEvents {
static StaticEvents() {
FieldInfo globalEventHandler = typeof(EditorApplication).GetField("globalEventHandler", BindingFlags.Static | BindingFlags.NonPublic);
var value = (EditorApplication.CallbackFunction)globalEventHandler.GetValue(null);
value += () => {
// Triggers for EVERYTHING
var e = Event.current;
if (e.type == EventType.KeyDown) {
if (e.keyCode == KeyCode.LeftShift || e.keyCode == KeyCode.RightShift) {
Debug.Log("SHIFT DOWN EVERYBODY!! - globalEventHandler");
}
}
};
globalEventHandler.SetValue(null, value);
}
}
[InitializeOnLoad]
public static class StaticEvents {
static StaticEvents() {
EditorApplication.hierarchyWindowItemOnGUI += (int instanceID, Rect selectionRect) => {
// Triggered only when a hierarchy item is selected + the hierarchy is focused
if (instanceID == Selection.activeInstanceID) {
var e = Event.current;
if (e.type == EventType.KeyDown) {
if (e.keyCode == KeyCode.LeftShift) {
Debug.Log("SHIFT DOWN EVERYBODY!! - hierarchyWindowItemOnGUI");
}
}
}
};
}
}
[InitializeOnLoad]
public static class StaticEvents {
static StaticEvents() {
EditorApplication.modifierKeysChanged += () => {
// No events, only "something" changed
};
}
}
[InitializeOnLoad]
public static class StaticEvents {
static StaticEvents() {
SceneView.onSceneGUIDelegate += view => {
// Mentioned in one thread: Get a control ID to not mess with other controls
// Does not seem to actually work in Unity 2018.1 on Linux - but theoretically should work
int controlID = GUIUtility.GetControlID(FocusType.Keyboard);
if (Event.current.GetTypeForControl(controlID) == EventType.KeyDown) {
if (Event.current.shift) {
Debug.Log("SHIFT DOWN EVERYBODY!! - onSceneGUIDelegate with Control ID A");
// Causes repaint & accepts event has been handled
Event.current.Use();
}
if (Event.current.keyCode == KeyCode.LeftShift) {
Debug.Log("SHIFT DOWN EVERYBODY!! - onSceneGUIDelegate with Control ID B");
// Causes repaint & accepts event has been handled
Event.current.Use();
}
}
};
}
}
[InitializeOnLoad]
public static class StaticEvents {
static StaticEvents() {
SceneView.onSceneGUIDelegate += view => {
// Same thing as variantA, but just directly process the event and not use it (watch only)
// Does not seem to actually work in Unity 2018.1 on Linux - but theoretically should work
var e = Event.current;
if (e == null) {
return;
}
if (e.type == EventType.KeyDown) {
if (e.keyCode == KeyCode.LeftShift) {
Debug.Log("SHIFT DOWN EVERYBODY!! - onSceneGUIDelegate");
}
}
if (e.modifiers.HasFlag(EventModifiers.Shift)) {
if (e.keyCode == KeyCode.LeftShift) {
Debug.Log("SHIFT DOWN EVERYBODY!! - onSceneGUIDelegate as Modifier");
}
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment