- Home /
How to make shortcuts using two keys
Hello everyone!
I'm currently working on a script that will handle some key shortcut. They have to be in like "ctrl + Key".
I already tried this solution found on the internet:
void Update () {
if (Input.GetKey(KeyCode.LeftControl)&&Input.GetKey(KeyCode.A))
{
//Do something
}
if (Input.GetKey(KeyCode.RightControl))
{
if (Input.GetKey(KeyCode.Z))
{
//Do something
}
}
}
Apparently it works only if I push the two keys at the same time wich is not very convenient.
So I would like to know if there was any other way to do it so you could push the key one after another in a given order? (CTRL and A, and not A and CTRL).
I hope I made myself clear as english is not my native language.
Thank you by advance!
Answer by Andrew_Kenady · Mar 28, 2014 at 04:01 PM
bool wasKeyADown;
void Update () {
if(Input.GetKey(Keycode.LeftControl) && Input.GetKey(Keycode.A) && !wasKeyADown)
{
//Have a party
}
wasKeyADown = Input.GetKey(KeyCode.A);
}
Thank you! When I first tried that didn't work, but I figured out that it was only because of the LeftControl $$anonymous$$ey. I tried with another one and it worked. Any idea why?
No problem :) This solution stores the last value of the 'A' key to make sure that it was pressed after the 'control' key, and not before.
Don't forget to mark my answer positive if it helped you out :)
Your answer
Follow this Question
Related Questions
How good is my "Quaternion" code efficiency? 1 Answer
Character cannot move Diagonally forwards 0 Answers
Get GameObject That Was Last Clicked? 2 Answers
Why is Input.GetAxisRaw() not returning whole numbers when using a joystick? 1 Answer
How to successfully apply force calling a function from another script 1 Answer