Help keyboard reconizing 2 players

Started by
11 comments, last by Kylotan 16 years, 10 months ago
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
•°*”˜˜”*°•.˜”*°•..•°*”˜.•°*”˜˜”*°•..•°*”˜˜”*°•.˜”*°•.˜”*°•. Mads .•°*”˜.•°*”˜.•°*”˜˜”*°•.˜”*°•..•°*”˜.•°*”˜.•°*”˜ ˜”*°•.˜”*°•.˜”*°•..•°*”˜I am going to live forever... or die trying!
Advertisement
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]
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.
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
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.
•°*”˜˜”*°•.˜”*°•..•°*”˜.•°*”˜˜”*°•..•°*”˜˜”*°•.˜”*°•.˜”*°•. Mads .•°*”˜.•°*”˜.•°*”˜˜”*°•.˜”*°•..•°*”˜.•°*”˜.•°*”˜ ˜”*°•.˜”*°•.˜”*°•..•°*”˜I am going to live forever... or die trying!
can anyone tell me what is causing this problem? or am i doing something wrong?
•°*”˜˜”*°•.˜”*°•..•°*”˜.•°*”˜˜”*°•..•°*”˜˜”*°•.˜”*°•.˜”*°•. Mads .•°*”˜.•°*”˜.•°*”˜˜”*°•.˜”*°•..•°*”˜.•°*”˜.•°*”˜ ˜”*°•.˜”*°•.˜”*°•..•°*”˜I am going to live forever... or die trying!
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
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.
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();}
•°*”˜˜”*°•.˜”*°•..•°*”˜.•°*”˜˜”*°•..•°*”˜˜”*°•.˜”*°•.˜”*°•. Mads .•°*”˜.•°*”˜.•°*”˜˜”*°•.˜”*°•..•°*”˜.•°*”˜.•°*”˜ ˜”*°•.˜”*°•.˜”*°•..•°*”˜I am going to live forever... or die trying!
For 'normal' letters, just quote them, like so: 'W', 'D', 'S', 'A' - instead of the VK_whatever stuff. Remember to use upper-case.
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
•°*”˜˜”*°•.˜”*°•..•°*”˜.•°*”˜˜”*°•..•°*”˜˜”*°•.˜”*°•.˜”*°•. Mads .•°*”˜.•°*”˜.•°*”˜˜”*°•.˜”*°•..•°*”˜.•°*”˜.•°*”˜ ˜”*°•.˜”*°•.˜”*°•..•°*”˜I am going to live forever... or die trying!

This topic is closed to new replies.

Advertisement