Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Event.current not detecting keys?!

Discussion in 'Scripting' started by CoolJosh3k, Aug 22, 2017.

  1. CoolJosh3k

    CoolJosh3k

    Joined:
    Dec 3, 2016
    Posts:
    145
    K, so this is seriously doing me head in...

    Code (CSharp):
    1. void OnSceneGUI() {
    2.     Debug.Log(Event.current.isKey);
    3. }
    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.
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,591
    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.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. [InitializeOnLoad]
    5. static class MyTest
    6. {
    7.     static MyTest()
    8.     {
    9.         SceneView.onSceneGUIDelegate += OnSceneGUI;
    10.     }
    11.  
    12.     static void OnSceneGUI(SceneView sceneView)
    13.     {
    14.         if (Event.current.isKey)
    15.             Debug.Log(Event.current.keyCode);
    16.     }
    17. }
     
    FinXzuOP likes this.
  3. CoolJosh3k

    CoolJosh3k

    Joined:
    Dec 3, 2016
    Posts:
    145
    I still get none for the keyCode. :(

    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...
     
    Last edited: Aug 23, 2017
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.