- Home /
Returning Which Key Was Pressed
Seems pretty simple but I can't find an answer anywhere.
All I want to do is get the key pressed.
Debug.Log(Input.string) gives me empty strings or sometimes if I type extra fast a few characters, but a simple keystroke it generally returns nothing. Maybe something to do with execution order.
void Update ()
{
if(Input.anyKeyDown)
{
Debug.Log(Input.inputString);
}
}
This is what I have so far. Would have thought it would have just logged the input string. I read somewhere that Input.anyKeyDown was internally invoked after Input.inputString so maybe it's just some sort of race condition.
Anyway any help would be most appreciated. Cheers :)
Strange. I would have expected it to work. You can "fix" your problem:
if(Input.inputString.Length > 0)
{
Debug.Log(">"+Input.inputString+"<");
}
Hi thanks Robertbu, that worked great. I'll use this with a combination of Input.any$$anonymous$$eydown (purely due to the fact that the bool condition will be a bit faster than check the string size each frame). Worked a charm thanks.
Answer by Jamora · Jul 13, 2013 at 11:05 AM
Because robertu posted his (correct) answer as a comment, I'll post an alternative solution.
Use Input.anyKey instead of Input.anyKeyDown.
Hi Jamora, thanks for the reply. However, Input.any$$anonymous$$ey will print the string for several frames, and I just want it to be printed only once. So in this case Roberts answer was just what I needed, but thanks for the feedback.
Your answer
Follow this Question
Related Questions
Get pressed key on smartphone 0 Answers
Very wierd issue with right arrow key 3 Answers
Multiple Input.GetKey 5 Answers
gui.button to enable keypress 2 Answers
Keycode else 1 Answer