Write the KeyCode directly in the inspector.

Hello,
I wonder if it was possible to create a string variable, to do an Input.GetKeyDown(KeyCode.myTouchString),
the interest would be to write the KeyCode directly in the inspector.

I don’t know when they added it because it hasn’t always been available but this works now.

Enum.TryParse(key, out code);
2 Likes

Ok, I got it like this:

if (Input.GetKeyDown(myVariableString))

but it doesn’t work for codes like Mouse0

We posted at the same time ^^ thank you Ryiah, but I don’t see how to integrate it with a condition.

I didn’t find it so I used this which is better ^^


if (Input.GetKeyDown(myVariable))```

Enum.TryParse returns a boolean indicating if the string value correctly parsed to the enum value:

string keyString = "Space";

if(Enum.TryParse(keyString, out KeyCode key))
{
  //"key" is the parsed KeyCode value.
  //In this example, its value should be KeyCode.Space.
}

But yes, I’d say this is a better solution anyway.

1 Like

Merci Vryken i will study this a little more ^^