Up Key

What’s the actual difference between:

if (Input.GetKeyDown(“up”))
{
print(“Up arrow pressed”);
}

and

if (Input.GetKeyDown(KeyCode.UpArrow))
{
print(“Up arrow pressed”);
}

because when I tried both, they both worked the same from what I tested (pressing the up arrow with both codes made the text print).

I believe using the string value makes the compiler have to refer to a table that translates the string value into the actual KeyCode reference. Whether this happens during compile time or at runtime, I don’t know, so there may or may not be a performance hit associated. The simplest answer would be that using the KeyCode value is more correct, and lends to stronger, more readable code than using the string equivalent. In the .NET world, it’s called Strong-Typing.

I did the same thing, tried them both and found out that upon using the KeyCode I got a list of suggestions from MonoDevelop for the key name I wanted to use, in this case the DownArrow. So it makes it a bit easier to find the name of the key you need instead of Googling for it’s input name all the time. More like a time efficiency difference.

Privacy & Terms