Sign in to follow this  
daviddiligent

two keys pressed together

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

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