Send a key to the Editor?

Simple, how to send a key or a combo of keys to the editor? So like:

Selection.objects = new Object[] { myObject; };
SendKey(KeyCode.LeftCtrl | KeyCode.D); // for duplicate

or

SendKey(KeyCode.F2); // for renaming

etc.

Is there some editor code that has this feature? If not, is there a platform-dependent way of doing this? or am I stuck with Winform and WinAPI stuff? (I’m on Windows)

Thanks :slight_smile:

I’ve just checked your other question, and here’s the solution to send keys to focused window:

var e = new Event { keyCode = KeyCode.F2, type = EventType.keyDown }; // or Event.KeyboardEvent("f2");
EditorWindow.focusedWindow.SendEvent(e);
EditorWindow.focusedWindow.SendEvent(Event.KeyboardEvent("x"));
EditorWindow.focusedWindow.SendEvent(Event.KeyboardEvent("y"));
EditorWindow.focusedWindow.SendEvent(Event.KeyboardEvent("z"));

AFAIK it isn’t possible to simulate input (i.e. create events from the void), despite I think you can send already triggered events on your current ScriptableObject to other ScriptableObjects. In your case, it seems you want the functionality behind those shortcuts.

For duplicate functionality over serializable objects check SerializedProperty.DuplicateCommand.
For renaming on the project window, check ProjectWindowUtil and EndNameAction (use MonoDevelop’s Assembly Browser to make things easier)