- Home /
Converting string of a key into KeyCode
Hi Everyone !
I've been browsing the web for around 2 days now about my problem.
I'm trying to convert a string (like "0", or "#") into a Keycode.
I have a number of key written in my scene, and I want to automatically check if they are pressed (for example if there is a "#" written, the code will go through the string of this ("#") and check each frame if this particular key is pressed).
I have found some solution but they didn't work (for example the code was trying to compare "Keypad0" to "0" so obviously it didn't match).
Anyone has an idea ?
Answer by ShadyProductions · Mar 11, 2019 at 04:36 PM
Something like this might work since the enum keycode uses the numbers of each char.
I also cached it here to prevent unnecessary enum parse calls.
private readonly Dictionary<char, KeyCode> _keycodeCache = new Dictionary<char, KeyCode>();
private KeyCode GetKeyCode(char character)
{
// Get from cache if it was taken before to prevent unnecessary enum parse
KeyCode code;
if (_keycodeCache.TryGetValue(character, out code)) return code;
// Cast to it's integer value
int alphaValue = character;
code = (KeyCode)Enum.Parse(typeof(KeyCode), alphaValue.ToString());
_keycodeCache.Add(character, code);
return code;
}
Thank you ! It worked perfectly for the numbers. I just noticed 3 things : For the letter "A" (for example), it didn't work (in the debugg it marked the letter as a number like "64"), same for "#", it didn't work as "#" is on the same key as "3" . And it doesn't seem to check for the numbers on the numeric keypad. Any idea in why an how to fix this ?
Hello, I am not sure what you mean int value = '#"
is 35 and $$anonymous$$eyCode.Hash
is 35
The method should correctly return $$anonymous$$eyCode.Hash
when char '#' is inserted.
Capital A is 65 and lowercase a is 97, $$anonymous$$eyCode.A
is mapped to 97 so it might be wise to apply .ToLower()
to your input first.
Yes I get "65" for Capital A and get "Hash" for "#" but it still doesn't detect if I press them. And for the numbers, it doesn't work for the numeric keypads numbers, only for the number on the top of the keyboard. For the latter, I am thinking about a code checking if the $$anonymous$$eyCode is "alpha"+number and "transfor$$anonymous$$g" it into "keyPad"+number. But I don't know if it is an efficient way to do it. For the first problem I have no idea.
Answer by xxmariofer · Mar 11, 2019 at 04:33 PM
you can compare it like this
Debug.Log(KeyCode.B.ToString().Equals("B"));
This would require a manual setup of each character case and seems like its a bit too much work. It's good when you have only a specific set of characters but not when you want to include every character.
your example is better and more complete, i just limit to answer the "compare "$$anonymous$$eypad0" to "0"" part.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
C# String Array Has Missing or Incorrect Keycode Strings 2 Answers
How To Switch A XmlNodeList To A String 1 Answer
Converting a string to an int 2 Answers