Test for multiple key presses?
Not sure if I could describe this well, but I'll give it a go.
I want to be able to chain KeyDown functions and give a result based on a certain combination.
For example: While "m" is held, test for the combination "n", "b", and "v" in that exact order, then when "m" is released print "NBV". However, if you just enter the combination "b" and "v" it prints "BV". And if nothing is pressed within one second, it print's "no input" and stops listening for any inputs forcing you to press "m" again.
I feel like this is a relatively simple function, but I don't know enough c# to do this, can anyone help me out?
Thank you for your time. Also, this is my first post!
Answer by zactanpeterson · Dec 01, 2019 at 04:47 AM
Well, you can conceptualize it so that's a good start, you want a while loop for M being pressed, the while loop can just be the Update method:
if(Input.GetKey(KeyCode.M)) { }
and in that loop, you can key get the key the was pressed that frame by using Input.inputString, store this in a string and you get the key sequence:
string output = "";
if(Input.GetKey(KeyCode.M)) { output += Input.inputString; }
since it will be also adding on the m as you hold it down you would put a check on it:
if(Input.GetKey(KeyCode.M)) { output += !Input.inputString.Equals("m") ? Input.inputString : ""; }
once you let go of m, print it out and reset output:
if(Input.GetKeyUp(KeyCode.M)) { if(!output.Equals("")) { Debug.Log(output); output = ""; } }
adding the timer is a little tricky but here's what I came up with:
string output = "";
float timer = 1;
bool holding = false;
private void Update()
{
if(Input.GetKeyDown(KeyCode.M))
{
holding = true;
}
if(holding)
{
output += !Input.inputString.Equals("m") && !Input.inputString.Equals(" ") ? Input.inputString : "";
timer -= Time.deltaTime;
}
if(Input.GetKeyUp(KeyCode.M) || timer <= 0)
{
if(!output.Equals(""))
{
Debug.Log(output);
output = "";
}
holding = false;
timer = 1;
}
}
it works pretty well, and it's easy to learn/alter if you wanted
Answer by moltensmail · Dec 01, 2019 at 04:48 PM
Hey @zactanpeterson!
This works great! Thanks for this, it was super helpful!
However, I was wondering if you could show me how I would go about doing something with this?
So, I would like to test for a certain value, and print a specific message, so for example:
string ap1 = "8787";
if (Input.GetKeyUp(KeyCode.M))
{
if (output == ap1) // Tests for the value "8787" in the output
{
print("hello!");
}
else // If the value is not entered it print's "unknown"
{
print("unknown");
}
holding = false;
timer = 1;
}
So that probably doesn't work, but it's a sketch of what I would like to try to do, do you think you could help me out?
Thank you for your time!
What you have there is good except for one thing, you can't use "==" on strings, ins$$anonymous$$d you would use:
if(output.Equals(ap1))
{
print("Hello");
}
Your way seems to work great the first time, but after that it always prints the else.
Here's what I have:
string ap1 = "8787";
if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.$$anonymous$$) || timer <= 0)
{
if (output.Equals(ap1))
{
print("Hello");
}
else
{
print("unknown");
}
holding = false;
timer = 1;
}
Not really sure what might be causing this, any ideas?