Key sequence input?

I’m trying to figure out how to get input from a sequence of keys, in other words, if the user presses on key “A” the script should wait for his next keystroke and do something based on that combination. The keys are not held down together, but one after the other. I’m trying to use a “yield” statement in javascript, but really need a “waitforkeystroke” function:

function Update () {
	if (Input.GetButtonUp ("FirstKey")) {
		FirstKeyFunction ();
	}
}

function FirstKeyFunction () {
	StartCoroutine("SecondKeyFunction");
	yield WaitForSeconds(5);
	StopCoroutine("SecondKeyFunction");
	print ("DONE");
}

function SecondKeyFunction () {
	while (true) {
		if (Input.GetButtonUp ("Number0")) {
			print ("the 0 key was released");
		}
		yield;
	}
}

Any suggestions?

So what you want to do is something like a code? If so then you could do something like:

var firstKey : String = H;
var secondKey : String = I;
var firstDown : boolean = false;
var allDown : boolean = false;

function Update() {
if(Input.GetKeyDown("firstKey") {
firstDown = true;
}
if(firstDown) {
if(Input.GetKeyDown("secondKey") {
allDown = true;
}
}
if(allDown) {
//Do Something
firstDown = false;
allDown = false;
}
}

I typed the code on the forum, so i am sorry for the lack of neatness.

Perfect, that’s super easy. Thanks! :slight_smile:

No problem, and something i just thought of is, you can do it so if they do something aside from pressing the second or third (or more) key, then make all of them false. I am not sure if it will work like this but something like:

var nextKey = String;

function Start() {
nextKey = firstKey;
}

if(Input.GetKeyDown("firstKey") {
nextKey = secondKey;
firstDown = true;
} 
if(Input.GetKeyDown!("nextKey")){
firstDown = false;
}

This code was typed in the forum thus not tested, so some tweaking may be necessary. At least you got the concept.

You can just yield on coroutines:

var keys = ["a", "b"];
private var keyIndex = 0;

function Start () {
	while (keyIndex < keys.Length) {
		yield WaitForKey();
		keyIndex++;
	}
	print("OK");
}

function WaitForKey () {
	while (!Input.GetKeyUp(keys[keyIndex])) yield;
}

–Eric

@xToxicInferno - You’re right, I didn’t think about that.

@Eric5h - I was hoping you’d show up and I knew you’d have a slick way of doing this. Thanks! :slight_smile: