two keys pressed together

Started by
6 comments, last by daviddiligent 15 years, 11 months ago
Hi guys. In my program, i want to allow user to use two keys to control the same thing. Say use both "U" and "J" to fire. So I wrote this in my code. But it does seem to work

if (g_keys->keyDown["U"] || g_keys->keyDown["J"])
     //do sth. here

Any suggestions?
/*----------------------------------------------------------------------------------------------------------------------------------*/Enthusiastic and wild about game development. Any opportunity would get me sink into any fantastic game-revolution era.
Advertisement
Does it work if you just press U and not work when you press J? Or it doesn't work for neither? And do you want the user to press both U and J at the same time to fire or either pressing U or J will fire?
I think it only worked when i pressed the former key, in this case U.

Sorry. It should be single quote in the code. 'U' not "U".

I want to use either key to control the fire, which means U can fire a bullet and so does J.

/*----------------------------------------------------------------------------------------------------------------------------------*/Enthusiastic and wild about game development. Any opportunity would get me sink into any fantastic game-revolution era.
Have you tried using && instead of ||?
Quote:Original post by DrakeThe
Have you tried using && instead of ||?


I haven't tried. I don't think it would work, because I want the keys work when either of them are pressed.
/*----------------------------------------------------------------------------------------------------------------------------------*/Enthusiastic and wild about game development. Any opportunity would get me sink into any fantastic game-revolution era.
Your code line is correct for the result you want to achieve, perhaps the problem lies within g_keys?
It looks like the code is correct but you're testing against only the UPPERCASE letters.

if (g_keys->keyDown["U"] || g_keys->keyDown["J"])     //do sth. here


so try this instead:

if (g_keys->keyDown['u'] || g_keys->keyDown['j'] || g_keys->keyDown['U'] || g_keys->keyDown['J'])     //do sth. here


When you press a key you're probably getting the lowercase version through.... maybe.

Andy

"Ars longa, vita brevis, occasio praeceps, experimentum periculosum, iudicium difficile"

"Life is short, [the] craft long, opportunity fleeting, experiment treacherous, judgement difficult."

1st of all, thanks for the reply.

My code was correct. Actually the reason it didn't work was because of this
if( u pressed or i pressed)   fire = true;if( u is not pressed or i is not pressed)   fire = false;


Because the second if actually made the fire to be always false, unless u and i were pressed together.

Solution: change the second if to if( u is not press AND i is not pressed).
/*----------------------------------------------------------------------------------------------------------------------------------*/Enthusiastic and wild about game development. Any opportunity would get me sink into any fantastic game-revolution era.

This topic is closed to new replies.

Advertisement