Sign in to follow this  
Tera_Dragon

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster   
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 this post


Link to post
Share on other sites
Guest Anonymous Poster   
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 this post


Link to post
Share on other sites
Guest Anonymous Poster   
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 this post


Link to post
Share on other sites
lhead    100
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?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this