# Detecting if two keys are held down together

## Recommended Posts

Tera_Dragon    260
I'm working on a text box for my gui. I would like to be able to select text when pressing shift and moving the caret with the arrow keys, though I am having trouble detecting when the shift key is being pressed. I've tried using an array of booleans, one for each key, and setting the corresponding boolean to true or false when I get the WM_KEYDOWN or WM_KEYUP message from windows. Though that doesn't seem to work for multiple keys pressed at a time. So I tried using GetAsyncKeyState(), but it doesn't seem to update correctly when the key is released. What I can do to get this to work?

##### Share on other sites
Anudh    110
That should be working. Having an array of booleans does detect multiple keys being pressed at the same time.

##### Share on other sites
Tera_Dragon    260
When I press the arrow key WM_KEYUP is called for the shift key.

##### Share on other sites
sepul    257
this code works for me :
		bool shift;		if( GetKeyState( VK_LSHIFT ) & 0x80 )	{			shift = true;		}		else	{			shift = false;		}

you can check that other keys are pressed with this method.

##### Share on other sites
Tera_Dragon    260
Thanks sepul that works nicely [smile]

##### Share on other sites
Guest Anonymous Poster
just wanted to point out that the max number of keys returned is 2 (downfall of keyboard input). So do not plan on using more than 3 or more key presses at once, or youll get the good ole keyboard beep.

##### Share on other sites
Tera_Dragon    260
Do you mean the max is three? As I'm sure there a quite a few three-key combos I use, ctrl+alt+del springs to mind.

##### Share on other sites
Guest Anonymous Poster
I believe Ctrl, Alt and Shift are handled separetly. That said, I'm not sure about his 2 key limit, maybe it's a Windows API thing? Either way it varies between keyboards also.

##### Share on other sites
Flarelocke    410
Pre-windows, there was a game that famously used a three key combo: I-L-M. So clearly there are at least some three key combos that work.

##### Share on other sites
Guest Anonymous Poster
Do this in any text editor your even this threads response box

1) put four+ fingers on the numerical part of your keyboard (1,2,3,4,5, ect..)

2) Now press these all of them down REALLY FAST at the same time and dont release them. youll see only two of them appear.

3) release them all at the same time (quickly), you will only see two others appear if your slow - none if your quick enough.

CONCLUSION: only two keys are processed at a time (from the main set of keys). I beleive that shift and the other control keys can be read transparantly along with these standard keys. If your ware using WASD control scheme however, then you are already taking away a simultanious keypress (move forward + action key = total available). This is probably why most of the "extra actions" in FPS and such use the control keys for their extra movements (and they are about in the right place. Im using a PS connected keyboard, so USB keyboards might differ. Ive had my applications beep at me if I press around 3+ keys (using windows messeges as the input handler).

##### Share on other sites
Same experience when you're playing splitscreen games, when two people use the same keyboard - whacky wheels, liero worms ...

Is there hope that this will be fixed some day, using USB or something to connect the keyboard? If anyone of you knows?