# ignoring simultaneous key presses

This topic is 2904 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey guys. I've been making 3d puzzle type app and have kinda run across a problem I can't seem to figure out. Basically, i want to prevent the user from hitting keys at once (or at least stop two more or less simultaneous key presses from registering). I thought I'd cracked it by locking it whilst it carries out one function and unlocking when done, however hitting two keys at the same time seems to override this and just do 2 things at once. searching the forums gives me some info as far as retrieving two simultaneous key presses but some seem to have their own drawbacks. e.g using an array of bools i can't differentiate between 'a' and 'A'..... any thoughts on this would be great. cheers peeps. ps the app is written using d3d 9 in c++....

##### Share on other sites
Can't you just have a variable that you set to true
once you start processing one key and as long as it
is true you won't start processing another. When it's
finished you set it to false again, allowing a new
key to be pressed.

Not knowing any specifics how you've set up your app
this may not be the best of solutions though.

##### Share on other sites
There is no such thing as "simultaneous". Key strokes are sent via messages which arrive sequentially one after the other, and in pairs. Each key down is paired with a key up event when the user releases the key. Therefore, as long as you have not received a key up event, you know that the respective key is still down.
Thus, if you want to disallow two or more keys being pressed at the same time, you only need to keep track how many keys are currently down.
One simple solution would be something like:

...
case WM_KEYDOWN:
if(count++ > 0) break;
DoSomethingWithKey();
break;
case WM_KEYUP:
--count;
break;
...

This does not take into account keys that you might want to handle differently or ignore, such as PrintScr or Shift, but that's an easy exercise.

##### Share on other sites
hmmm yeah that would probably be the easiest way of doing it but unfortunately i dunno that that's gonna cut it. I tried to do something similar in the class I'm using.

As it stands at the moment the function called returns immediately
case WM_CHAR:             switch (wParam)             { 				        //vertical split twisting		case 't':	        	nbc.animTwist(XAX, LEFT, CLOCKWISE);		break;		case 'T':			nbc.animTwist(XAX, LEFT, ANTICLOCKWISE);		break;            .            .            .

but doesn't finish what it's doing for like 20 frames. which is why I check in animTwist(...) whether it's locked or not. this does work after a fashion but if you hit 2 keys at the same time it just seems to ignore this and do both at once, i guess this could point to a problem with my code in my nbc class cos messages are never received at exactly the same time right?
thanks for the response by the way, appreciate it

edit: I've just seen samoth's post that snuck in while i was typing this, that makes lots of sense and should sort this. i'll have a fiddle with my wndproc and see how that works out. cheers for that...

##### Share on other sites
One thing I forgot to mention is key repeat. If the user keeps the 't' key down, it may fire some extra events after a while which a simple incrementing counter won't catch properly. But you can filter those out with the repeat-count field in lParam. Or, you can just have a second variable that holds the "current key" and ignores further KEY_DOWNs on that one.

Or, if you're not afraid of being a bit intrusive, you could just turn off key repeat whenever your game has the focus. This is normally a definite no for an application to do, but for a game it may be acceptable, provided you make sure it reliably restores the original and thus doesn't screw up other apps.

##### Share on other sites
thanks for that samoth, it was exactly what i was after. I'm glad you mentioned the key repeat bit too because after adapting my code playing around a bit holding down any one key does screw it over a bit but i can sort that. cheers for the responses both of ya, much appreciated.