• Advertisement
Sign in to follow this  

two keys pressed together

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Your code line is correct for the result you want to achieve, perhaps the problem lies within g_keys?

Share this post


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

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement