- Home /
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
Answer by vexe · Dec 27, 2013 at 03:49 PM
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[i];
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) 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.
sir can you help me..my game is like 4 pis 1 word..i have random value..how can i set the getkey in every letter..? is this one is applicable?
Answer by T27M · Dec 25, 2013 at 10:16 PM
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"))
yes that's exactly what I am trying to do, but how can I make it possible to change it, do i have to enter each combination in the Input tab? or is there a way to access that data and change it via a script? also thanks for the fast response
When you build and run the game unity launches the input window seen in the link above. The user can customize the input settings from there. You don't have to change any code as you are using the name to refer to the button/key pressed.
If you want to do it during play time you would have to code something up which saves the new key layout. Take a look at this answer which uses the Event class to detect key presses.
There's also this, but I haven't used it so I can't comment on how good it is.
The thing is that I am saving the controls of the user in a database (by using Event.Get$$anonymous$$ey) now I need to extract the data which is a string and convert it to keycode so that unity would recognize it.
Ok, I understand. Are you positive that "W" is at the correct array number? I tested your code with a local list and it's working fine for me.
Yes I am positive, I have used the Debug.Log to check what it has as data and W is the only thing that comes up. Can you please show me the exact code you have used so I can check any mistakes I may have done. Thanks a lot
Your answer
Follow this Question
Related Questions
KeyCode parse problem 0 Answers
Key binding screen 0 Answers
Replacing the Input Manager Asset with your own 2 Answers
Unity built-in axes - how do they work? 1 Answer
Multiple Cars not working 1 Answer