Help keyboard reconizing 2 players

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

Recommended Posts

im trying to allow 2 players to share the keyboard, one player using "a(left),w(up),s(down),d(right) and the other useses the arrows. Heres the code im using:
[source lang = "cpp"]void HandleKeys()
{
if (!g_bGameOver)
{
// Steer the blue light cycle in response to arrow key presses
if (GetAsyncKeyState(VK_UP) < 0)
SteerCycle(0, 0);
else if (GetAsyncKeyState(VK_RIGHT) < 0)
SteerCycle(0, 1);
else if (GetAsyncKeyState(VK_DOWN) < 0)
SteerCycle(0, 2);
else if (GetAsyncKeyState(VK_LEFT) < 0)
SteerCycle(0, 3);
// Steer the red light cycle
if(GetAsyncKeyState(58) < 0)
SteerCycle(1,0);
else if(GetAsyncKeyState(44) < 0)
SteerCycle(1,1);
else if(GetAsyncKeyState(53) < 0)
SteerCycle(1,2);
else if(GetAsyncKeyState(41) < 0)
SteerCycle(1,3);
}
else if (GetAsyncKeyState(VK_RETURN) < 0)
NewGame();
}

But when the game plays, the game dosent respond to the red light cycle commands, only the blue cycles. the SteerCycle() function 1st parameter is what bike it is, 2nd parameter is what image(what way the bike should be facing) will be shown. Its probaly not that, that cause the problem. according to(as far as i could read) MSDN, 58 should be equal to W, 44 is D, 53 is S and 41 is A. Thanks in advice! , Mads

Share on other sites
list of virtual key codes.

Try using 'GetAsyncKeyState( VKUP ) & 0x80' instead of < 0, like so:

if( GetAsyncKeyState( VK_W ) & 0x80 ) ){}

I hope that was helpful [smile]

Share on other sites
Also note, GetAsyncKeyState can only reliably tell you if the button is pressed at the time the function is called - not whether it was pressed in-between function calls.

Although it is possible to also check if it was pressed in-between calls by using:

GetAsyncKeyState(VK) & 0x01

The above is unreliable as Windows' threading model may allow another application to receive the 0x01 flag, and thus your application will be told that no, a key has not been pressed - even if one has.

Share on other sites
yea, i tried VK_W and so forth at first, as it seemed most logicly, however i tells me that those are undeclared identifiers, so i figured it wasnt the correct approach.
ERRORS:
Quote:
 Error 1 error C2065: 'VK_W' : undeclared identifier c:\Documents and Settings\mads\Dokumenter\Visual Studio 2005\Projects\light cycles\light cycles\LightCycles.cpp 148Error 2 error C2065: 'VK_D' : undeclared identifier c:\Documents and Settings\mads\Dokumenter\Visual Studio 2005\Projects\light cycles\light cycles\LightCycles.cpp 150Error 3 error C2065: 'VK_S' : undeclared identifier c:\Documents and Settings\mads\Dokumenter\Visual Studio 2005\Projects\light cycles\light cycles\LightCycles.cpp 152Error 4 error C2065: 'VK_A' : undeclared identifier c:\Documents and Settings\mads\Dokumenter\Visual Studio 2005\Projects\light cycles\light cycles\LightCycles.cpp 154

im confused..
Also..what is that "& 0x001" thing? In my current case i dont think it is a problem as the code above gets called 30 times a second, however id like to implement it anyway.

Share on other sites
can anyone tell me what is causing this problem? or am i doing something wrong?

Share on other sites
i think the problem could be that you're using

if(condition)..

else if(condition)..

..

if the blue light cycle player is pushing a button, it'll bail the if..else set and not check for the red light cycle players buttons.

consider using

if(condition)...

if(condition)...

Share on other sites
Also of note may be the fact that many older keyboards do not detect more than three keys being pressed at a time; if your machine is emitting beeps it's a pretty good chance this is what's going on.

Share on other sites
isnt (inside the (!g_bGameOver)) dosent it sees the blue and the red cycles as 'not related'?, i mean, all the blue cycles commands is associated with if, and else if, and the red cycle's command is associated with annother(seperate) if sentence? or am i misunderstanding something. Anyhow, im still getting these errors:
Quote:
 Error 1 error C2065: 'VK_W' : undeclared identifier c:\Documents and Settings\mads\Dokumenter\Visual Studio 2005\Projects\light cycles\light cycles\LightCycles.cpp 148Error 2 error C2065: 'VK_D' : undeclared identifier c:\Documents and Settings\mads\Dokumenter\Visual Studio 2005\Projects\light cycles\light cycles\LightCycles.cpp 150Error 3 error C2065: 'VK_S' : undeclared identifier c:\Documents and Settings\mads\Dokumenter\Visual Studio 2005\Projects\light cycles\light cycles\LightCycles.cpp 152Error 4 error C2065: 'VK_A' : undeclared identifier c:\Documents and Settings\mads\Dokumenter\Visual Studio 2005\Projects\light cycles\light cycles\LightCycles.cpp 154

in this code:
[source lang = "cpp"]void HandleKeys(){  if (!g_bGameOver)  {    // Steer the blue light cycle in response to arrow key presses    if (GetAsyncKeyState(VK_UP) < 0)      SteerCycle(0, 0);    else if(GetAsyncKeyState(VK_RIGHT) < 0)      SteerCycle(0, 1);    else if (GetAsyncKeyState(VK_DOWN) < 0)      SteerCycle(0, 2);    else if (GetAsyncKeyState(VK_LEFT) < 0)      SteerCycle(0, 3);        if(GetAsyncKeyState(VK_W) < 0)     SteerCycle(1,0);    else if(GetAsyncKeyState(VK_D) < 0)     SteerCycle(1,1);    else if(GetAsyncKeyState(VK_S) < 0)     SteerCycle(1,2);    else if(GetAsyncKeyState(VK_A) < 0)     SteerCycle(1,3);  }  if (GetAsyncKeyState(VK_RETURN) < 0)    NewGame();}

Share on other sites
For 'normal' letters, just quote them, like so: 'W', 'D', 'S', 'A' - instead of the VK_whatever stuff. Remember to use upper-case.

Share on other sites
Thanks! :) Someone posted above that i could check for keyboard input, even when the function isnt running, how do i know what to use similair to 00x1? for all keyboard inputs

Share on other sites
It's exactly the same as posted already.

GetAsyncKeyState('A') & 0x01

Share on other sites
Thanks :) wasnt sure if that was legal to do on all letters (eg. another letter could have 0x002) thanks for clearing it up :D

Share on other sites
The GetAsyncKeyState call returns the current state of the key in question. Every key has the same potential return values. 0x80 corresponds to the 'currently pressed' bit, and 0x01 corresponds to the 'was pressed since last time' bit.

Share on other sites

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

Create an account

Register a new account

• Forum Statistics

• Total Topics
628639
• Total Posts
2983972

• 10
• 18
• 20
• 13
• 9