- Home /
Input.GetKeyDown(KeyCode.Alpha) as an integer?
Hi!
Is there an built-in option to take Input Alpha buttons that represents number keys as an integer?
So, for example, instead of doing this:
if (Input.GetKeyDown(KeyCode.Alpha1))
ExecuteFunction(1);
else if (Input.GetKeyDown(KeyCode.Alpha2))
ExecuteFunction(2);
Where "ExecuteFunction" is any function that takes integer.
Do something like this:
ExecuteFunction(/*Input as an integer*/);
Or the only way to achieve this is to create custom class for that particular case?
If there is any source that you can link in - that's great, since I couldn't find.
Thank you in advance.
Answer by Captain_Pineapple · May 25, 2020 at 02:07 PM
you could write this a bit more simple like this:
for(int i=0;i<10;i++)
{
if(Input.GetKeyDown((KeyCode)(48+i)))
{
ExecuteFunction(i);
}
}
this is possible since a KeyCode is just an enum and can thus be simply casted into an integer (or as in the code the other way around) As you can see when hovering over the KeyCode.Alpha0
it has the value 48. All following numbers are just assigned to the following numbers upwards.
That's actually great workaround, I'll take it x)
Thank you very much!
cool, just realized though that you'd have to modify this with an break;
since your OP contained a if/else statement. The workaround in my Answer only works as if statement as it is.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Getting the text from UIInput 1 Answer
Projection to mouse position in isometric game 1 Answer
How to make shortcuts using two keys 1 Answer