Dynamic KeyCode Unity Exception

Hi, I am trying to make a simple input manager using C# and I got stuck in the KeyCode thing, here is the code

if (Input.GetKeyDown ((KeyCode)Enum.Parse(typeof(KeyCode), _controls[2]) ))
{}

The _controls is a list of type string and at this moment in the [2] position I have “W”.

And every time I start the game it gives me this exception “UnityException: Input Key named: W is unknown”

Can you please Help me with this problem Thanks

Are you trying to let the user change the key to use?

Unity has a built in input manager that can already does that.

Edit → Project Settings → Input

if (Input.GetKeyDown("MyCustomInputName"))

I ran into this problem in a game jam we did - it was about a typing game. I ran into the very same exception. One fundamental thing I didn’t realize at first, is that checking for if (Input.GetKeyDown("w)) will also return true if “W” was pressed (capital W) - In this case, if you want to detect if it was a capital or small, you’d have to check for the shift key as well with if (Input.GetKey(KeyCode.LeftShift))

Here’s how I approached detecting the letters, I made an array of all the characters, and I looped over them and see which letter was pressed, I then check for the shift, as mentioned earlier:

private string[] keys =
	{
		"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
		"1", "2", "3", "4", "5", "6", "7", "8", "9", "0",
		".", ",", "/", ">", "<"
	};

void Update()
{
	for (int i = 0; i < keys.Length; i++) {
		var key = keys*;*
  •   	if (Input.GetKeyDown(key)) {*
    
  •   		print(key + " was pressed");*
    
  •   		if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))*
    
  •   		      print("and it's a capital " + key);*
    
  •   	}*
    
  • }*
    Not the prettiest, I know.
    Then my partner @Jamora notified me of Input.inputString ([doc][1]) which I haven’t got the time to test out - but it should act like, getch in C++ - It just returns the string that the user typed.
    Hope that helps.
    [1]: Unity - Scripting API: Input.inputString