Here is my issue, i am trying to get a KeyCode from a string variable.
When I put the string inside the line, here “Z”, the Parse method works well like the example below :
But when I get the KeyCode from a string variable (here fwdInput.text is equal to the same value “Z” and it is a string), the program process it in a different way, and I don’t understand, i’m getting this error :
Assuming that “fwdInput.text” is a string, I don’t see how the program could be “processing it in a different way”. I think it is more likely that there’s something weird going on with the value of that string–that is, it’s not really “Z”, like your string literal a few lines above.
For example, maybe the letter is some unicode character that looks similar to Z, but isn’t the same code point. Or maybe your string actually contains more than one character, but some of them are invisible or unprintable, like a zero-width space.
I would try converting both “Z” and your input string into bytes (using some unicode encoding) and check if they match at that level.
Thanks it is different like you thought. The string “Z” is equal to byte[2] and the variable is byte[4]. Do you know a way to make the variable like the string “Z”?
To do exactly the thing you said (make the variable like “Z”) is easy: fwdInput.text= "Z";
But presumably you are really thinking of this as just one example of a more general problem, which is “cleaning” your input, so that whatever input you get is turned into whichever key is “equivalent” in some specific sense. To do that–or even to figure out whether you can do that–you’re almost certainly going to need to collect more information about exactly why and how your input is “dirty” in the first place.
Telling me that it’s byte[4] is super vague. You haven’t said which encoding scheme you used (UTF16? UTF8? Something else?) or what the values of those four bytes are (you effectively just told me the length).
It could be that you have an actual “Z” with some whitespace attached, in which case probably all you need to do is call Trim. But there are still lots of other problems that would be consistent with the information you’ve posted so far.
I finally found the solution. Thanks a lot for your help!
I was using a TextMeshPro object instead of a Text object.
The Text is from the package UnityEngine.UI and the TextMeshPro from the TMP package, that’s why it wasn’t working. I was not using UnityEngine so I couldn’t transform a TextMeshPro into a KeyCode.
Thanks again for your answers, I discovered how to use encoding and the Trim method. I really appreciate it =)