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