Event.current not detecting keys?!

K, so this is seriously doing me head in…

void OnSceneGUI() {
    Debug.Log(Event.current.isKey);
}

All I am getting is “none” over and over, no matter what I try.

I have tried other variations too, such as looking at Event.current and Event.current.type. No such luck.

I am running version 2017.1.0p4.

I tested the following code in 2017.1.0p4, works for me. Pressing a key while the Scene View has focus, prints the corresponding key-code.

using UnityEngine;
using UnityEditor;

[InitializeOnLoad]
static class MyTest
{
    static MyTest()
    {
        SceneView.onSceneGUIDelegate += OnSceneGUI;
    }

    static void OnSceneGUI(SceneView sceneView)
    {
        if (Event.current.isKey)
            Debug.Log(Event.current.keyCode);
    }
}
1 Like

I still get none for the keyCode. :frowning:

Could you explain how this is intended to work? Why is it needed?

I get the feeling that I should not need to use this trick, based on it not being exposed in the API Documentation.

edit: Okay, so if I do not have anything active in the hierarchy it works…

OnGUI uses an intermediate mode system. That means that OnGUI will fire twice per event (one layout and one for the actual event). Which can end up being even more times per key press if a repaint is involved.

The point of Event.current is to figure out which call you are in. Otherwise any code you execute will happen multiple times, which you normally don’t want.

This video is useful if you want to get deeper into intermediate mode GUI.

https://www.youtube.com/watch?v=RletYTFpBP8