- Home /
Why is GetKeyDown returning false when provided with string from .inputString?
I'm coding a key tracker function in the purpose to trace if any key was double-klicked. For now, I want it to check what key was last pressed and if that key was pressed this frame.
When using the approach below, GetKeyDown seems to return 'false' even though keyDown are providing a string. Any hints on why this approach doesn't work?
var keyDown : String = Input.inputString;
if(keyDown != String.Empty && Input.GetKeyDown(keyDown)){
Debug.Log(keyDown);
}
Answer by aldonaletto · Dec 08, 2012 at 11:12 PM
Apparently, internally Input.inputString is updated after GetKeyDown is executed, thus keyDown is empty when you read Input.GetKeyDown. For what do you need this? Tell us, and maybe someone can suggest you another approach.
NOTE: Ooops! You've already said what's your intention! Let me think for a while and soon I'll return with something.
EDITED: inputString is only updated one frame after the key is actually pressed, thus using GetKey functions is somewhat complicated. But there's a simple solution for this case: when inputString isn't empty, compare its value to the last key pressed: if it's the same, check the time elapsed since last key - if it's lower than some predefined limit, you have a double click:
var tDoubleKey: float = 0.25;
private var lastKey: String;
private var timer: float;
function Update(){
if (timer > 0){ // if timer > 0, decrement it
timer -= Time.deltaTime;
}
var key = Input.inputString;
if (key != ""){ // if any key pressed check double click:
// if key = last key and double click time not elapsed...
if (key == lastKey && timer > 0){
// we've got a double click!
print(key+" double-pressed");
} else { // otherwise update lastKey and restart timer
lastKey = key;
timer = tDoubleKey;
}
}
}
Answer by Bunny83 · Dec 08, 2012 at 11:58 PM
You could use OnGUI to detect keyinput, however, like i said somewhere here, OnGUI wraps the OS input and when you hold down a key it will be repeated automatically at the repeat-rate configured in your OS. So you have to detect both, KeyDown and KeyUp
var key : KeyCode;
var releasedOnce = false;
function OnGUI()
{
var e = Event.current;
if (e.type == EventType.KeyDown)
{
if (!releasedOnce)
{
key = e.keyCode;
}
else if (key == e.keyCode)
{
releasedOnce = false;
Debug.Log("You pressed " + key + " two times in a row");
}
else
{
releasedOnce = false;
key = e.keyCode;
}
}
else if (e.type == EventType.KeyUp && e.keyCode == key)
{
releasedOnce = true;
}
}
Can't guarantee that there are no syntax errors. I wrote this from scratch and usually i don't use UnityScript.
edit
This does also work:
var key : String = "";
function Update()
{
var keyDown = Input.inputString;
if (keyDown != String.Empty)
{
key = keyDown;
}
if(key != "" && Input.GetKeyDown(key))
{
Debug.Log(key+ " pressed twice ");
key = "";
}
}
The problem with InputString is that it is filled one frame after the actual keypress occurrs AND is only set for one frame. It's automatically cleared after that frame.
it's a great pity you cannot get unique EventType.$$anonymous$$eyDown in OnGUI ... typically when writing for a finger device, you have stupid #if UNITY_EDITOR code so that it works to some extent on the bench. It's annoying that you cannot just simply get "the key just pressed, once" in OnGUI. As far as I know there's no simple way to say in OnGUI "key X was just pressed once"
Your answer
Follow this Question
Related Questions
how can i use getkey and getkeydown for two diffrent functions. 2 Answers
Play half of Animation on KeyDown/Other Half on KeyUp 1 Answer
When holding down shift cant use the "a" key, an extremely weird bug! 2 Answers
Canvas is preventing Input.GetKeyDown from working. 0 Answers
Simple Quick Question 2 Answers