Detecting if two keys are held down together

Started by
10 comments, last by Zahlman 18 years, 1 month ago
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?
____________________________________________________________Programmers Resource Central
Advertisement
That should be working. Having an array of booleans does detect multiple keys being pressed at the same time.
-[Anudhyan][Website]
When I press the arrow key WM_KEYUP is called for the shift key.
____________________________________________________________Programmers Resource Central
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.

dark-hammer engine - http://www.hmrengine.com

Thanks sepul that works nicely [smile]
____________________________________________________________Programmers Resource Central
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.
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.
____________________________________________________________Programmers Resource Central
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.
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.
---New infokeeps brain running;must gas up!
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).








This topic is closed to new replies.

Advertisement