Sign in to follow this  
MadsGustaf

Help keyboard reconizing 2 players

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


Link to post
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 this post


Link to post
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 148
Error 2 error C2065: 'VK_D' : undeclared identifier c:\Documents and Settings\mads\Dokumenter\Visual Studio 2005\Projects\light cycles\light cycles\LightCycles.cpp 150
Error 3 error C2065: 'VK_S' : undeclared identifier c:\Documents and Settings\mads\Dokumenter\Visual Studio 2005\Projects\light cycles\light cycles\LightCycles.cpp 152
Error 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 this post


Link to post
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)...

instead of if..else

Share this post


Link to post
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 this post


Link to post
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 148
Error 2 error C2065: 'VK_D' : undeclared identifier c:\Documents and Settings\mads\Dokumenter\Visual Studio 2005\Projects\light cycles\light cycles\LightCycles.cpp 150
Error 3 error C2065: 'VK_S' : undeclared identifier c:\Documents and Settings\mads\Dokumenter\Visual Studio 2005\Projects\light cycles\light cycles\LightCycles.cpp 152
Error 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 this post


Link to post
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 this post


Link to post
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 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