- Home /
Error sound on key input EditorWindow OnGUI()
Hi guys,
I'm doing an EditorWindow inherited class, and I need User input such as arrow keys pressed at any time.
Knowing OnGUI() can only listen to Event.current.isKey for such input, I'm using that (though I'd rather not since it causes issues with window must have focus)
I've crossed quite a lot of sync-barriers by placing
if (Event.current != null && Event.current.isKey)
{
Repaint();
}
at the top of void OnGUI()
.. However one thing I struggle with, is that apparently since there's no input field Unity is returning an system error beep every time a key is pressed.
At least on Mac, have not even tried on Win yet, but even on one platform it's a drag, I'd really like my Editor script not to inform the user he's doing something wrong when he's not :)
Would you know how to 'catch' the input (like it's done in input fields) so no error sounds are returned.. or even better; A (hack) to not rely on Event.current.isKey for user input in EditorWindow?
Thanks! :)
Answer by Bunny83 · Sep 15, 2016 at 07:52 PM
I don't see that behaviour on Windows. Maybe it's a Mac specific issue...
Have you actually tried "eating" the event?
Event.current.Use();
btw: You don't have to check if Event.current is null. OnGUI is only called when an event is set. Also keep in mind there are several "key" events that might fire for a single key. You always get at least a key down and a key up. "isKey" is just a shorthand for testing if the current event type is one of the key messages.
EditorWindows can also handle events inside the sceneview. For that you can subscribe a method to the static event SceneView.onSceneGUIDelegate
.
But since we have no idea what's the point of your editor window, suggesting things it kind of pointless ^^.
@Bunny83 : This is pure gold - you've helped me a lot! Thanks.
What I want to do: $$anonymous$$y daily work is often involving Unity effects. This often involves prefabs with multiple GameObjects each with materials, each with textures..
So for instance if I have an old Efx, and I'd like to use a modified version, it is hell as I cannot just go and change the material or the texture of a copy, since they share with the other Efx.
Using Unity's Window Locks and dragging around can be a very time consu$$anonymous$$g task, totally killing creativity.
That is one task I'm now solving with an asset.
Another is a smooth way of solving the apparently ever returning issue of programmers making things as missiles (example) that destroys themselves, and artists who think the missilles smoke should not disappear before it is faded out. (to say it simple)
$$anonymous$$aking complex powerups, missiles that spawn children etc can be an overly complex task - and I'm trying to make it overly simple :)
So, I have done a tool, and it's working, and working great, but I need to polish it before releasing it.
Therefore I have run straight into the funny world of mixing #if UNITY_EDITOR EditorWindow Directory.GetFiles AssetDatabase.LoadAssetsAtPath
and now also trying to refactor with Events and 'what users would expect to be able to do with keys' in $$anonymous$$d..
P$$anonymous$$ me if interested, and thanks again! :)
Facepalm!
I figured you mentioned SceneView with reference to me looking for ways to catch key input without having EditorWindow in focus. But..
SceneView is (another) undocumented class.. With that as a starting point I'm now trying to learn how to work with it from EditorWindow..
Any hints on how to listen to key input via SceneView would be very, very welcome. Thanks.
Answer by Fritsl · Sep 16, 2016 at 12:58 PM
Ha, got it, here's how to get key input from Scene View via EditorWindow:
public class YourClass : EditorWindow
void OnEnable() {
SceneView.onSceneGUIDelegate += this.OnSceneGUI; }
void OnDisable() { SceneView.onSceneGUIDelegate -= this.OnSceneGUI; }
void OnSceneGUI(SceneView sceneView)
{
Event e = Event.current;
if (e.isKey)
Debug.Log("Detected key code from Scene View: " + e.keyCode);
}