Why can we use (KeyCode.Alpha1 + index) to get other KeyCode.Alpha?

I am curious why it works in this way.
I have tried to Debug.Log(KeyCode.Alpha1), the Console only shows UnityEngine.Debug.Log(Object) to me.
If KeyCode.Alpha1 is not a simple int, why can we do " + int " and makes Alpha2, Alpha3 and so on to work successfully?

Thanks!

1 Like

Hi,

That’s a very good question. Press the ctrl key in Visual Studio and then on KeyCode in your script. A long list should open. Scroll down to Alpha1. I’m sure when you see it, you’ll immediately understand why Rick’s solution works. :slight_smile:

2 Likes

Thanks for guiding me to look at the table, which is interesting.
Now I see that Alpha1 = 49
However, I found that below’s modifications lead to error:

  1. Input.GetKeyDown(49 + index)
  2. Input.GetKeyDown(KeyCode.49 + index)

There is no KeyCode.49 in that list, thus you cannot access it. Only existing KeyCode values work.

So what you have here is that KeyCode is a enumeration which is a type in itself separate from int although it holds a int value. So while KeyCode.Alpha1 will give you a int of value 49 , you cant pass a int directly into the Input.GeyKeyDown function itself. This is because the function GeyKeyDown has 2 versions and one of them expects a value of type string while the other one expects a value of type KeyCode. So as you see there is no version of GeyKeyDown that accepts a regular int - recall that you need to match the type of the methods parameters when passing values to it.

With the above in mind looking at your examples we see :

  1. 49+index - this gives a “int” which is not of type KeyCode. but KeyCode.Alpha1, Alpha2, etc are as they are declared in the enumeration. So you can only pass in a value of type KeyCode to that function not a int. But you can add a int to a Keycode value as that also gives a keycode if the sum is declared in the keycode.

  2. Keycode.49 - is incorrect , there is no member declared with name “49” in the enumeration KeyCode . Also you cant have member names starting with digits so this would not be a legal/allowed name anyway.

Why use enums instead of directly using 48 or 49 etc ? Well for one thing it allows for safety. You are able to make sure the function cant be called with some invalid int value which is not a valid keycode. So if you try a Keycode.Alpha1 + 2000 it will give you a error.

Edit:
First 2 arrows show the 2 versions of GetKeyDown and arrow #3 shows the error in Visual studio when I try to check if KeyCode.Alpha1 is equal to 49 which they are not as they are different types. ie, one is of type KeyCode and the other of type int

image

1 Like

Thanks for the deep and clear explanation!

I have the next question.
If the int of KeyCode.Alpha2 is not 1 larger than KeyCode.Alpha1, maybe 100 or something, can we make use of string as enum and put it back for calling KeyCode?
For example, using “numberOfAlpha” for the number after KeyCode.Alpha, and assign a new value to it, then make use for a new KeyCode.Alpha

Glad that helped.

I dont quite understand this new question but I will say this.
You can only use a name that is declared in the enum declaration . If the value you want to use is equal to Alpha1 (int value 49) + 100 which would be 149 then there should a member with value set to 149 in the enum’s declaration.
As it happens there is no member defined with value 149 in the KeyCode enumeration.
But If there was some member defined with value 149 called “nameofthatmember” you could have used either KeyCode.nameofthatmember or KeyCode.Alpha1 + 100.

I recommend you research C# enumerations.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms