简体   繁体   中英

How to know what key is pressed using switch in C# Unity

trying to use Switch to know what key is pressed on Unity using C# and Visual Studio but it's not working for me. Here is my statement

switch (Input.GetKeyDown)
    {
        case (KeyCode.UpArrow):
            Debug.Log("Up Arrow key was pressed");
            break;
        case (KeyCode.DownArrow):
            Debug.Log("Down Arrow key was pressed");
            break;
        case (KeyCode.KeypadEnter):
            Debug.Log("Enter key was pressed");
            break;
    }

The accepted answer is very inefficient.

You're both using reflection ~60 times a second & looping through loads of things you don't need to loop through, just for the sake of using a switch case instead of if .

The way you should be checking for input is:

if (Input.GetKeyDown(KeyCode.ArrowUp)) 
    Debug.Log("Up Arrow clicked");
if (Input.GetKeyDown(KeyCode.ArrowDown)) 
   Debug.Log("Up down clicked");
if (Input.GetKeyDown(KeyCode.KeypadEnter)) 
    Debug.Log("Enter clicked");

I think this is what you're looking for? this gets the value of the key clicked, however you must debug log the inputValue to know what is coming you cannot use KeyCode.UpArrow in the below, and this might be the only way you can use switch with input.

    var inputValue = Input.inputString;
    switch(inputValue) {
    case ("1"):
        Debug.Log("1 key was pressed");
        break;
    case ("2"):
        Debug.Log("2 key was pressed");
        break;
    case ("3"):
        Debug.Log("3 key was pressed");
        break;
}

You can iterate through all input by using System.Enum.GetValues()

 (using System.Linq;)

void Update() {
    var allKeys = System.Enum.GetValues(typeof(KeyCode)).Cast<KeyCode>();
    foreach (var key in allKeys) {
        if (Input.GetKeyDown(key)) {
            Debug.Log(key + " was pressed.");
        }
    }
}

As for the switch statement, it's not possible and not worth it if you're searching for specific input.
Would be best to just use if-else statements.

I would do it as follows:

void Update()
{
   if (Input.anyKeyDown)
   {
       if (Input.GetKeyDown(KeyCode.Up)
          Debug.Log("Up Arrow key was pressed");
       else if (Input.GetKeyDown(KeyCode.Right)
          Debug.Log("Right Arrow key was pressed");
       else if (Input.GetKeyDown(KeyCode.Down)
          Debug.Log("Down Arrow key was pressed");
       else if (Input.GetKeyDown(KeyCode.Left)
          Debug.Log("Left Arrow key was pressed");
    }
}

maybe you would want a bunch of ifs instead of "else if"

or you could explore a way to make this work:

void OnGUI()
{
    var input = Event.current;
    switch (input.keyCode)
    {
        case KeyCode.Up:
            Debug.Log("Up Arrow key was pressed");
            break;
        case KeyCode.Right:
            Debug.Log("Right Arrow key was pressed");
            break;
        case KeyCode.Down:
            Debug.Log("Down Arrow key was pressed");
            break;
        case KeyCode.Left:
            Debug.Log("Left Arrow key was pressed");
            break;
     }

but in my experience it glitches, there is certainly a way to do it correctly but this code in my project its not stable as it runs multiple times while you press the key once.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM