SDL_GetKeyState question

How can I read two keys being pressed simultaneously. I need to know all
the steps necessary to make this happen.

Thanks,
Dave

How can I read two keys being pressed simultaneously. I need to know all
the steps necessary to make this happen.

Uint8 *keys;
    keys = SDL_GetKeyState(NULL);

    if( keys[SDLK_a] == SDL_PRESSED )
    {
       // Do something
    }

This will give you what any key is doing at any time.
Look in the documentation for the other constant definitions for the keys.________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com

I know this much. I need to read two keys at once and it never happens
with the following code

Uint8 * keys;
keys = SDL_GetKeyState(NULL);
if (keys[SDLK_SPACE] == SDL_PRESSED && keys[SDLK_LEFT] == SDL_PRESSED)

this never works when I run it in a loop. Single key presses do work
however. I need a techniqure for multiple keys at a time.

Dave> >How can I read two keys being pressed simultaneously. I need to know all

the steps necessary to make this happen.

Uint8 *keys;
keys = SDL_GetKeyState(NULL);

    if( keys[SDLK_a] == SDL_PRESSED )
    {
       // Do something
    }

This will give you what any key is doing at any time.
Look in the documentation for the other constant definitions for the keys.


Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com

How can I read two keys being pressed simultaneously. I need to know all
the steps necessary to make this happen.

Uint8 *keys;
keys = SDL_GetKeyState(NULL);

    if( keys[SDLK_a] == SDL_PRESSED )
    {
       // Do something
    }

This will give you what any key is doing at any time.

Don’t forget to call SDL_PumpEvents() or another event polling function
beforehand, as SDL_GetKeyState() doesn’t update the internal keystate,
just returns what SDL already knows about.

See ya!
-Sam Lantinga, Lead Programmer, Loki Entertainment Software

I am also calling SDL_PumpEvents. It still doesn’t work.
here is the code.

Everything that checks for one key at a time works great. Anything that
checks multiple keys never occurs.

Uint8 * keys;

keys = SDL_GetKeyState(NULL);
while (keys[SDLK_ESCAPE] != SDL_PRESSED) {
SDL_PumpEvents();
keys=SDL_GetKeyState(NULL);
if (keys[SDLK_RIGHT] == SDL_PRESSED){
ship.curFrame(2);
if (ship.rect()->w + x + 8 < 640) {
x += 8;
ship.blitSprite(screen, x, y);
}
}
//Move Left

else if (keys[SDLK_LEFT] == SDL_PRESSED) {
        ship.curFrame(1);
        if (x - 8 > 0) {
            x -= 8;
            ship.blitSprite(screen, x, y);
            }
    }

//Shot fires
else if (keys[SDLK_SPACE] == SDL_PRESSED) {
if (!hasShot) {
hasShot = true;
shotx = x + (ship.rect()->w / 2) - (ship.rect()->w/2);
shoty = y - (ship.rect()->h + 1);
}
}
else if (keys[SDLK_SPACE] == SDL_PRESSED && keys[SDLK_RIGHT] ==
SDL_PRESSED)
{
ship.curFrame(2);
if (ship.rect()->w + x + 8 < 640) {
x += 8;
ship.blitSprite(screen, x, y);
}
if (!hasShot) {
hasShot = true;
shotx = x + (ship.rect()->w / 2) - (ship.rect()->w/2);
shoty = y - (ship.rect()->h + 1);
}
}

}

Dave wrote:

I am also calling SDL_PumpEvents. It still doesn’t work.
here is the code.

Everything that checks for one key at a time works great. Anything that
checks multiple keys never occurs.

if (keys[SDLK_RIGHT] == SDL_PRESSED){
else if (keys[SDLK_LEFT] == SDL_PRESSED) {
else if (keys[SDLK_SPACE] == SDL_PRESSED) {
else if (keys[SDLK_SPACE] == SDL_PRESSED && keys[SDLK_RIGHT] ==
SDL_PRESSED)

Only one of these if/else if blocks will be executed…
Take out the 'else’s.

JW

Dave wrote:

I am also calling SDL_PumpEvents. It still doesn’t work.
here is the code.

Everything that checks for one key at a time works great. Anything that
checks multiple keys never occurs.
if (keys[SDLK_RIGHT] == SDL_PRESSED){
… [CUT]
else if (keys[SDLK_SPACE] == SDL_PRESSED && keys[SDLK_RIGHT] ==

You first test on right, which is true. Your code never reaches
the second test.

Bye,
Johns–
Become famous, earn no money, create graphics for FreeCraft.

http://FreeCraft.Org - A free fantasy real-time strategy game engine
http://fgp.cjb.net - The FreeCraft Graphics Project

Dave wrote:

Everything that checks for one key at a time works great.
Anything that checks multiple keys never occurs.

if (keys[SDLK_RIGHT] == SDL_PRESSED) {
// move right
}else if …

else if (keys[SDLK_SPACE] == SDL_PRESSED &&
keys[SDLK_RIGHT] == SDL_PRESSED) {
// move right and fire shot
{

If “space && right” is true then only “move right” is executed at the
beginning of the checks. The “if/else” statement executes sequentially and
bails out on the first true condition. If you really want to use "if/else"
you have to do the “space && right” check at the beginning.

The way the code is laid out, it looks like you might be better off doing a
series of “if” statements rather than the “if/else” stuff as follows:

if (keys[SDLK_RIGHT] == SDL_PRESSED) { /* move right */ }
if (keys[SDLK_LEFT] == SDL_PRESSED) { /* move left */ }
if (keys[SDLK_SPACE] == SDL_PRESSED) { /* fire shot */ }

The combinations will take care of themselves.

  • Randi

Regimental Command
Generic Armored Combat System
http://www-users.cs.umn.edu/~relander/regcom/index.html

Dave wrote:

I am also calling SDL_PumpEvents. It still doesn’t work.
here is the code.

Everything that checks for one key at a time works great. Anything that
checks multiple keys never occurs.

if (keys[SDLK_RIGHT] == SDL_PRESSED){
else if (keys[SDLK_LEFT] == SDL_PRESSED) {
else if (keys[SDLK_SPACE] == SDL_PRESSED) {
else if (keys[SDLK_SPACE] == SDL_PRESSED && keys[SDLK_RIGHT] ==
SDL_PRESSED)

Only one of these if/else if blocks will be executed…
Take out the 'else’s.
OOoops. My bad. Thanks. I haven’t had to do any serious programming in
a year or so. (no excuse stupid me!!!)

daveOn Tue, 2 May 2000, John Watson wrote:

JW