Hi,
Input.inputString is the way to go however it can be somewhat tricky to get your head around.
https://docs.unity3d.com/ScriptReference/Input-inputString.html
I’m just starting to get this I think so hopefully i can explain it well.
Remember that the Update() function/method gets called once per frame. You can have many frames each second in your game. (FPS = frames per second and maybe it is 60 frames each second). Also, we can type pretty fast so i think it’s possible to type more than one character per frame so keep that in mind.
Input.anyKeyDown I think is more for just detecting if the user pressed a key and then doing some action (like ‘to continue, press spacebar’), not really finding out which key it was. I could be wrong here but any time i tried it in my solution is just messed things up. There is something with it and how it operates between frames/Update() function calls that made it seem not suited for this task.
In your first code example here: https://gist.github.com/anonymous/86fcdf714411cf34c3f1a4e990d0addb
your Update() function detects if any key was pressed. If so, then it assigns a value to the UI component using the text.text line.
text.text = Input.inputString + “Key pressed!”;
First, this is overwritting any previous text stored in text.text.
To retain accumulated text over multiple frames you need to add to existing text like this:
text.text = text.text + Input.inputString + " Key pressed!";
That still wont work though because it was wrapped by the conditional If statement checking for anykeydown which just messes this all up.
So i learned something at this point, thanks! I thought a more elegant solution like shown in the Input.inputString Unity documentation would be needed. Turns out that’s not the case. The simplest solution i found towards the effect you want is just one line in the update() function
void Update () {
text.text = text.text + Input.inputString;
}
http://pastebin.com/Xig36L4D
Your second code example where you try the class string variable called ‘pressedkey’ has the same problems as the first try. AnyKeyDown seems to mess things up and you overwrite any previously stored text by not reassigning it along with the new text added afterwards.
If you are interested in how the Unity documentation shows the use of Input.inputString i have a fully commented and working example of that as well which i did before i found the simplest solution, lol. This does allow the uses of backspace however which is pretty cool. And also to perform an action when they press enter which i used in the last section to collect user range values.
http://pastebin.com/d5Ntrjya
Same code, without the comments: http://pastebin.com/HHpR7Wty
Hope this all helps.