Advertisement

Multiple Key Presses

Started by November 20, 1999 06:11 AM
3 comments, last by Slayer-X 25 years, 1 month ago
If you're using DirectInput, there's a very similar post here called "Player input: buffered or immediate" that handles the same problem.
What if I decide to make my own keyboard input function and forget about direct input.
Advertisement
Perhaps this will help you:
int ASCIIKeys[256];
int SpecialKeys[256]; // keys like up down..

void AsciiKeyPressed(unsigned char key)
{ ASCIIKeys[key]=1;
}
void AsciiKeyReleased(unsigned char key)
{ ASCIIKeys[key]=0;
}

void SpecialKeyPressed(unsigned int key)
{ SpecialKeys[key]=1;
}
void SpecialKeyReleased(unsigned int key)
{ SpecialKeys[key]=0;
}

in your gameloop you now have to do the following:
...
if(SpecialKeys[FIRE]) player.firegun();
if(SpecialKeys[UP]) player.run();
...

The only problem you now have is to make your programm call the callback functions from above when a keyboard event happens.

How do you get multiple key presses and process them in your game? Example: Getting the forward key being presses and the fire key being pressed and making the player fire and walk at the same time.
if you are using windows:

#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)

and then...

if (KEY_DOWN(VK_RETURN))
{
// return pressed
}

This topic is closed to new replies.

Advertisement