Multikey handling

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

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>
Jimmy’s World.org
-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20030526/0d5755ad/attachment.pgp

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

Great!!
I m an idiot… Indeed, you can in the event keydown, check all the key u want…

Thanxs :-)----- Original Message -----
From: Jimmy
To: SDL Mailing List
Sent: Monday, May 26, 2003 8:31 PM
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

This SDL_GetKeyState is yeah like the deviceGetDeviceState from DirectX. I like this way, cause you don’t have to iterate the events… and u can check easier the state of the keyboard…

Thanxs----- Original Message -----
From: Atrix Wolfe
To: sdl at libsdl.org
Sent: Monday, May 26, 2003 8:44 PM
Subject: Re: [SDL] Multikey handling

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

Helo
As i have seen on some examples, and i been testing, you can use the keys this way:

First you need an Uint8 variable, you can call it jus t"keys".

Uint8 keys;

Then, in the game loop, you can “cach” the keys this way:

keys = SDL_GetKeyState(NULL); // Get Keys will “detetect” when a key is pressed
// the SDLK_ pressed will go inside “keys”.

if (keys[SDLK_UP]) { Y-=1; }
if (keys[SDLK_DOWN]) { Y+=1; }
if (keys[SDLK_LEFT]) { X-=1; }
if (keys[SDLK_RIGHT]) { X+=1}

With this you can have for example, UP ARROW key and LEFT arrow key pressed at the same time, and a sprite for example, moving UP and LEFT at the same time.

So, if you define a STRAFE key and a FIRE key and also Movement Keys, you will be able to Move, fireing, in strafe mode. At the same time.

This will go INSIDE the game loop.

Hope this will help :slight_smile:

                       Eduardo Garcia Rajo (h)------------------------------------------------------------------

Visite: http://www.solucion-digital.com.ar
SOLUCION DIGITAL
Redes - Software - Servicios

----- Original Message -----
From: shimrod
To: sdl at libsdl.org
Sent: Monday, May 26, 2003 3:00 PM
Subject: [SDL] Multikey handling

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