-1

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;
    }
Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
3Bady
  • 183
  • 3
  • 15
  • 1
    "It is not working" has never been sufficient error description. What do you expect to happen? What is happening or not happening? How and where is that code even called? **Is** the code even called? – Christopher Jul 20 '19 at 14:04
  • 1
    @3Bady Christopher is right, you did not ask the question properly. Please edit your question instead of getting rude. – Lajos Arpad Jul 20 '19 at 14:41

4 Answers4

2

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");
Fredrik Schön
  • 4,888
  • 1
  • 21
  • 32
1

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.

Lyrca
  • 528
  • 2
  • 15
0

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;
}
Odai Amer
  • 34
  • 4
  • 1
    can you please tell me what is the error and what is the value coming from the code above? – Odai Amer Jul 20 '19 at 14:24
  • 1
    `String` is not equal to `KeyCode`... You'll have to `Debug.Log(Input.inputString)` to see what up-arrow represents – Fredrik Schön Jul 20 '19 at 14:32
  • I have updated my answer, this should work please read it again 3Bady and let me know what happens. – Odai Amer Jul 20 '19 at 15:09
  • When I put the cursor on DownArrow it shows me Cannot implicity convert type 'UnityEngine.KeyCode to 'string'', when I put the cursor KeyCode this message come to me KeyCode returned by Event.keyCode. These map directly to physical key on keyboard. – 3Bady Jul 20 '19 at 15:26
  • You're answer after update works well, but still when I press up or down or enter no message come to me in the console – 3Bady Jul 20 '19 at 15:28
  • try what Fredrik mentioned earlier Debug.Log(Input.inputString) just so you can tell what is the value for enter, up, down and so on.. – Odai Amer Jul 20 '19 at 16:19
0

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.

Barreto
  • 374
  • 2
  • 14