Add a non-menu-item keyboard shortcut to an editor script

Allright, I’ve researched this for the last hour, and zero luck. I have found how to hotkey/shortcut to a menu item, but zero answers on how to do this simply within an editor script.

For example, my script shows a gizmo in the scene view. I would like the user to press “t” to toggle on/off that gizmo, at any time.

Now, I can get “t” to work if I check the event from “OnGUI”, but of course that only works while the editorwindow with my script is selected. Not very hand, since the user would have to keep re-selecting the editor window before pressing the shortcut key.

There absolutely must be a way to do this…but I am at a complete loss. Any help here? Thanks much!!

Easiest way would be to make a menu item that just enable/disables the gizmo on the object in question.

Reference page

1 Like
void OnInspectorGUI//in editor
void OnGUI()//mono
    {
        Event e = Event.current;
        if (e.keyCode==KeyCode.T){
do something
}
}

if you want to capture global events not local for ur focused Editor Window, or Mono, then you can define shortcut thru

. and use Shortcut Attribute tag to bind to listener function Unity - Scripting API: ShortcutAttribute

Oh, wow, a 7 year necro with exactly the solution the OP was not looking for. What’s up with all the undead lately? Is it Z Week?