- Home /
Detecting if 2 Buttons are pressed at the same time
I am trying to make it so that you are not able to press more then one button at a time. If you do I want to put be able to detect it and have a bool saying multiple buttons are pressed. I have it set up so that if more then one button is pushed the character doesn't move. That is done by
if (input.getkeydown(Keycode.uparrow) && !input.getkeydown(Keycode.Rightarrow)){
//movement code
}
That is just a portion of the code, showing how I did it, I have more buttons and all but one has a !. I would like to have a simple bool that is can just put into an if statement if possible.
What are you trying to do by knowing if multiple buttons are pressed? Do you want to tell the player "you're pushing multiple buttons, that's not ok" or do you simply want your input to process only one input?
The reason is that I want to make it harder to move, there is no diagonal movement in the game so when two buttons are pressed I want to make a red circle with a line through it appear and there will be no movement. Basically the idea is in the heat of the moment you panic and try to press more then one button you won't move and an enemy will get you. How would you reset the number count after buttons are released?
Answer by Jarnak · Jun 19, 2015 at 02:53 PM
Well, you could put this in your Update(). There is surely a better way, but it's the first that came into my mind.
int count = 0;
bool tooManyKey = false;
foreach (KeyCode kc in System.Enum.GetValues(typeof(KeyCode))
{
if (Input.GetKeyDown(kc))
count++;
if (count>1)
{
tooManyKey = true;
break;
}
}
I don't know why do you need that, but maybe you should better use GetKey instead of GetKeyDown. If you use GetKeyDown, the bool will turn true only if you simultaneously press 2 buttons, not if you hold one and then press another. It's your choice to make x)
This does not reset the count back to 0 when no buttons are pressed so too$$anonymous$$any$$anonymous$$ey will always be active after 2 keys are pressed.
I wrote "you could put this in your Update()", so yes too$$anonymous$$any$$anonymous$$ey will always be active during the frame it was activated. Not so long so.
Answer by Baste · Jun 19, 2015 at 11:02 AM
A big question here is "why?". You usually want to allow the player to press several buttons at once. Also, GetButtonDown only registers on the frame the button is pressed - which means that two of them being true only happens if the player manages to press them approximately at the same time. That being said, this is not very difficult to code up:
bool rightPressed = Input.GetKey(KeyCode.RightArrow);
bool leftPressed = Input.GetKey(KeyCode.LeftArrow);
bool upPressed = Input.GetKey(KeyCode.UpArrow);
bool downPressed = Input.GetKey(KeyCode.DownArrow);
int numPressed = 0;
if (rightPressed)
numPressed++;
if (leftPressed)
numPressed++;
if (upPressed)
numPressed++;
if (downPressed)
numPressed++;
if (numPressed == 1) {
if (upPressed) {
MoveUp();
}
...
}
Answer by LaneFox · Jun 19, 2015 at 04:07 PM
Assuming the keys are known, I would use a static bool to gate the input logic.
public class Keys : MonoBehaviour
{
public static bool SomeKeyIsDown;
void Update()
{
if (!SomeKeyIsDown && Input.GetKeyDown(KeyCode.LeftArrow))
{
SomeKeyIsDown = true;
transform.position = (transform.position + Vector3.left*Time.deltaTime);
}
if (!SomeKeyIsDown && Input.GetKeyDown(KeyCode.RightArrow))
{
SomeKeyIsDown = true;
transform.position = (transform.position + Vector3.right*Time.deltaTime);
}
// etc...
else SomeKeyIsDown = false;
}
}
}
You could also make a static method to control the bool if you didn't want to toggle it in each input condition.
public class KeyCheck : MonoBehaviour
{
public static bool SomeKeyIsDown;
void Update()
{
SomeKeyIsDown =
Input.GetKeyDown(KeyCode.RightArrow) |
Input.GetKeyDown(KeyCode.LeftArrow) |
Input.GetKeyDown(KeyCode.UpArrow) |
Input.GetKeyDown(KeyCode.DownArrow);
// etc...
}
}
I guess there are problems with doing this exactly as is, but you get the idea.
Your answer
Follow this Question
Related Questions
How can I use multiple GetButtonDown keys simultaneously? 2 Answers
Having GetButton act like GetKeyDown 1 Answer
Simultaneous button presses 2 Answers
Having trouble checking if the player is in a trigger. 2 Answers
Having issues with Input.GetKey... 3 Answers