Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Use Event class outside of OnGUI()

Discussion in 'Scripting' started by RoeeHerzovich, Jan 25, 2021.

  1. RoeeHerzovich

    RoeeHerzovich

    Joined:
    Apr 12, 2020
    Posts:
    103
    So I'd like to detect when any key is pressed and determine which key.

    I could just do:

    Code (CSharp):
    1. if (Input.anyKey) {
    2.     foreach (var key in Enum.GetValues(typeof(KeyCode)))
    3.         if (Input.GetKeyDown(key))
    4. }
    (I am not sure about the Enum.GetValues exactly as I just wrote it in here without actually looking in C# but you get my intention)

    This will get me a result however looping through all of them(yeah I can just filter out things like joystick and touch to reduce the time and get like.. 50-ish keys instead of like 300 but still) so I don't quite like this solution...

    Or I could do:
    Code (CSharp):
    1. if (Input.anyKey)
    2.     string keys = Input.inputString;
    That'll work without a loop however it will only detect strings, thus pressing the CTRL/Shift buttons etc will result in an empty string thus not being detected.


    I know Event can detect it using Event.current.keyCode however it can only be used inside of the OnGUI function...

    My question is: Can I somehow use the Event class properly outside of the OnGUI OR find a good usage similar to the Event's for my problem?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    I just loop the enum itself and check each one for going down:

    Code (csharp):
    1.         foreach( var kc in System.Enum.GetValues(typeof(KeyCode)))
    2.         {
    3.               if (Input.GetKeyDown(kc))
    4.               {
    5.               }
    6.         }
    Remember that GetKeyDown() is only valid in Update() and functions called from Update(), nowhere else.
     
  3. RoeeHerzovich

    RoeeHerzovich

    Joined:
    Apr 12, 2020
    Posts:
    103

    I already wrote that solution there saying why I do not prefer it, in the worst case I will use it, but my question is if there is a better solution
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    My bad, skipped over it, I guess I don't have a problem with code that actually works. :)

    Good luck in your search.
     
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,528
    Well, it depends ^^. You can simply use OnGUI if you want to receive events. I don't really see an issue with that. Is there a concrete reason why you don't want to use an OnGUI callback?

    Apart from that there is indeed a way to receive events without the OnGUI callback. However that is internally used by the new UI system. So when you use it yourself, you will consume the events and they can not be processed by the UI. The method is called Event.PopEvent It's used directly inside the InputField to process keyboard events when the field is selected. As you can see the method removes the event from the queue so there is only ever one class that can receive the events through this method. So using OnGUI would make much more sense.