Receive any Keyboard input and use with Switch statement on Unity

后端 未结 3 1332
萌比男神i
萌比男神i 2021-01-23 04:58

I don\'t want to use if statement. For simplicity and performance I want to use switch case and execute that method. I want all the keyboard Input to get detected. But is there

相关标签:
3条回答
  • 2021-01-23 05:26

    I've not used Unity, however, I understand your problem and I am a C# developer.

    From a quick search I have found someone on Unity forums with a similar problem to you. Here is the thread https://answers.unity.com/questions/1520939/check-if-there-is-a-keyboard-input.html.

    if (Input.anyKeyDown)
     {
         Event e = Event.current;
         if (e.isKey)
         {
             Debug.Log("Current Key is : " + e.keyCode.ToString());
         }
     }
    

    The code above (from the Unity forum link) enables you to detect input.anyKeyDown (keyboard and mouse). Then you can filter the mouse detections by checking if the input was only a keyboard input with e.isKey

    Here is documentation for KeyCode. This also includes all the properties available to it (many keyboard related properties that you can potential check against).

    For example (not tested):

    Event e = Event.current;
    if (e.isKey)
    {
        Debug.Log("Current Key is : " + e.keyCode.ToString());
    
        if(e.keyCode == KeyCode.A) //checks if the keycode returned equals the 'A' key
        {
             // Do something
        }
    }
    

    EDIT: As mentioned by the other answer you can try Input.inputString. According to the documentation insputString contains "Only ASCII characters". So for example you could do something like this in the 'Update' method if letters was what you were only looking to check.

    void Update()
    {
        //get the input
        var input = Input.inputString;
    
        //ignore null input to avoid unnecessary computation
        if (!string.IsNullOrEmpty(input))
        {
            switch(input)
            {
                case 'a': break;
                case 'b': break;
            }
        }
    }
    

    Hope this can help.

    0 讨论(0)
  • 2021-01-23 05:38

    Best Solution - Use Input.inputString

    You may use Input.inputString to catch the keyboard input entered in the last frame as a string.

    void Update()
    {
        //get the input
        var input = Input.inputString;
        //ignore null input to avoid unnecessary computation
        if (!string.IsNullOrEmpty(input))
            //logic related to the char pressed
            Debug.Log("Pressed char: " + Input.inputString);
    }
    

    Other solutions

    Foreach Loop

    You might use foreach loop approach, like suggested in other answers, but it is not as performant. Foreach allocates more memory and must check against all possible keys (it is also less readable).

    If you do not care about time and memory performance, then you might follow this answer. It uses the foreach loop approach and create a reusable interface.


    OnGUI

    Finally you could catch the Event.current (that is a unityGUI Event), like explained in this answer, but doing so you would rely on the OnGUI method. This method has the worst performance.

    0 讨论(0)
  • 2021-01-23 05:43
    foreach(KeyCode kcode in Enum.GetValues(typeof(KeyCode)))
    {
        if (Input.GetKey(kcode))
        Debug.Log("KeyCode down: " + kcode);
    }
    

    Also you can cache the value of Enum.GetValues(typeof(KeyCode)) for optimization.

    0 讨论(0)
提交回复
热议问题