[Help] Recognising any key press

I wanted to expand on this lesson’s concept of changing text.text when the space bar is pressed by making the code detect which key I pressed and changing the text output accordingly. Google’s told me that Input.inputString was the way to go here, so this is what I came up with:

The Monodevelop compiler and Unity console both seem to like that code, but when I run it, the text usually just changes to “key pressed!” without the key I actually pressed. However, if I press two keys at once, it’ll occasionally work and insert one of them into the text. This happens with any combination of alphanumeric and punctuation keys, with and without Shift. Exactly the same thing was happening in the Unity console when I added “print (Input.inputstring + " key pressed!”);" to line 18.

I thought of declaring a public string variable on line 8 (“public string pressedkey”) and defining the input as a string that way, for example like so:

if (Input.anyKeyDown) {
pressedkey = Input.inputString;
text.text = pressedkey + " key pressed!";
}

but (perhaps predictably) I get exactly the same result. Any idea why this is happening, or what I need to add to the code?

Try this:

foreach (KeyCode vKey in System.Enum.GetValues(typeof(KeyCode))) {
			if (Input.GetKey (vKey) ) { Debug.Log( vKey )}
         	}

This should give you the key pressed. you might have to do some type changes to get the string to work.

OK, I gave that a quick go, and I think you’re right that I’ll have to change something - text.text now immediately becomes “Joystick4Button19 key pressed” when I run the program. I don’t have a joystick plugged in…

I’m still confused as to why the code in my original post only works as intended when I press two keys at once (and even then rarely), though. I’m clearly misunderstanding something about Input.inputString - I thought it detected me pressing a key and returned that keypress as a string. What am I missing?

Sorry new to this…
but I’ve only just noticed your link to GitHub - your code is truncated and I never noticed the option to extend.

I tried your code and it works! The only thing missing is a closing } but I imagine that just a cut and paste error to GitHub.

I am running Unity 5.4.2f2

Well, it’s still not working on my laptop, but it’s reassuring to know that it’s working for someone! Just to confirm, it’s printing, for example, “q key pressed” or “@ key pressed” when you press the relevant button (case-sensitive, of course)?

I’ve just noticed the missing } in the github file. That is a copy+paste error on my part - the Monodevelop code definitely has it, and I don’t think it’d let me run the program if it wasn’t there!

I’m running Unity 4.7.2f1 at the moment, as the course suggested to use Unity 4.x for these early sections. Do you think that could have something to do with it?

My version is case sensitive and displays characters such as @ and :

I was naughty and went straight for the latest version. It could be a to do with the version. Try your code out when you upgrade maybe.

That sounds like a good idea to me. I’ll leave the thread on [Help], in case some clever clogs has a better idea, and come back to this later on (if I remember!).

I liked your LSD text adventure, by the way. I overthought the first question because of an old joke I know, but those were some nice brainteasers!

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.