yeah, or you can use SDL_GetKeyState to get a pointer to the buffer where SDL stores the states of the keys. This code for instance would let you control a player in 2d where pressing up and left would make them move diagonaly (but pressing left and right at the same time would make the player not move at all):
char * Keys;
int PlayerX,PlayerY;
Keys=SDL_GetKeyState(0);
if(Keys[SDLK_LEFT])
PlayerX–;
if(Keys[SDLK_RIGHT])
PlayerX++;
if(Keys[SDLK_UP])
PlayerY–;
if(Keys[SDLK_DOWN])
PlayerY++;
etc
i like Jimmy’s way better since you can have players bind keys/joysticks/mouse events to game events but this is sometimes better for simple tasks just to use the state buffer already provided----- Original Message -----
From: Jimmy
To: SDL Mailing List
Sent: Monday, May 26, 2003 11:31 AM
Subject: Re: [SDL] Multikey handling
Events come twice for each keystroke, once when the key is pressed and again when the key is released.
If you want a nice flat list like it sounds like DirectX has, just make an array of states and set them true/false with each key event, then you can test against those states.
enum {
Forward,
Backward,
Left,
Right
}
bool states[4] = { false,false,false,false };
// Later,
while ( SDL_PollEvent( &event ) )
switch (event.type) {
case SDL_KEYDOWN:
if (event.key.keysym.sym==SDLK_LEFT) states[ Left ] = true;
if (event.key.keysym.sym==SDLK_RIGHT) states[ Right ] = true;
// etc…
break;
// Do the same for SDL_KEYUP, setting the state to false
}
// Are they going up and right?
if (states[Forward] && states[Right]) { /* Do forward right thingy */ }
This is pretty basic, but that’s the concept. The nice thing about a system like this is you can create dynamic mapping from A to B where A is something and B is a state. It allows you to later consider the states independently of how they got set. So a ‘forward’ state getting set could happen because of a mouse motion, button click, key press, joystick or game script.
On Mon, 2003-05-26 at 14:00, shimrod wrote:
Hi,
I'm just discovering SDL. I'm trying to understand the keyboard handling.
The tutorial shows us how to catch key by key using the events... But how do you do to catch multiple keys pressed on the keyboard ( upper mouvement, with strafe and shoot for example... ).
With directx SDk, you can have a buffer, with the state of the device in a time T, so you can test the buffer for each key before you iterate.
thanks for the help
--
Jimmy <jimmy at jimmysworld.org>
Jimmy's World.org