Multiple keypresses

I’m having trouble detecting 3 or more keypresses. My code looks something
like this:

int handle_key_press (SDL_KeyboardEvent key_event) {
switch (key->keysym.sym) {
/
Set flags for pressed keys here */
}
}

int handle_key_release (SDL_KeyboardEvent key_event) {
switch (key->keysym.sym) {
/
Clear flags for released keys here */
}
}

int main () {
/* Initialization stuff */

    while (!exit_flag) {
            while (SDL_PollEvent (&main_event)) {
                    switch (main_event.type) {
                    case SDL_KEYDOWN:
                            handle_key_press (&main_event.key);
                            break;
                    case SDL_KEYUP:
                            handle_key_release (&main_event.key);
                            break;
                    default:
                            break;
                    }
            }
    }

    return 0;

}

This worked great for a while, until I needed 3 keys at a time (left/right,
w/s and space), and the third key is never detected. What am I doing wrong?–

  • Mik Mifflin

I had a similar problem with the SDL version of Stella (Atari 2600
emulator) I’m working on. But my problem was that some 3-key
combinations worked while others didn’t. I spent a few hours going over
the code and looking in FAQ’s, but didn’t find anything.

Then I tried the code on another machine and it worked!! The problem was
that my crappy keyboard couldn’t detect certain 3-key sequences >:( And
it only took me 3 hours to figure that out !!!

SteveOn January 13, 2002 04:49 pm, you wrote:

I’m having trouble detecting 3 or more keypresses. My code looks
something like this:

int handle_key_press (SDL_KeyboardEvent key_event) {
switch (key->keysym.sym) {
/
Set flags for pressed keys here */
}
}

int handle_key_release (SDL_KeyboardEvent key_event) {
switch (key->keysym.sym) {
/
Clear flags for released keys here */
}
}

int main () {
/* Initialization stuff */

    while (!exit_flag) {
            while (SDL_PollEvent (&main_event)) {
                    switch (main_event.type) {
                    case SDL_KEYDOWN:
                            handle_key_press (&main_event.key);
                            break;
                    case SDL_KEYUP:
                            handle_key_release (&main_event.key);
                            break;
                    default:
                            break;
                    }
            }
    }

    return 0;

}

This worked great for a while, until I needed 3 keys at a time
(left/right, w/s and space), and the third key is never detected. What
am I doing wrong?

Stephen Anthony wrote:> On January 13, 2002 04:49 pm, you wrote:

I’m having trouble detecting 3 or more keypresses. My code looks
something like this:

int handle_key_press (SDL_KeyboardEvent key_event) {
switch (key->keysym.sym) {
/
Set flags for pressed keys here */
}
}

int handle_key_release (SDL_KeyboardEvent key_event) {
switch (key->keysym.sym) {
/
Clear flags for released keys here */
}
}

int main () {
/* Initialization stuff */

    while (!exit_flag) {
            while (SDL_PollEvent (&main_event)) {
                    switch (main_event.type) {
                    case SDL_KEYDOWN:
                            handle_key_press (&main_event.key);
                            break;
                    case SDL_KEYUP:
                            handle_key_release (&main_event.key);
                            break;
                    default:
                            break;
                    }
            }
    }

    return 0;

}

This worked great for a while, until I needed 3 keys at a time
(left/right, w/s and space), and the third key is never detected. What
am I doing wrong?

I had a similar problem with the SDL version of Stella (Atari 2600
emulator) I’m working on. But my problem was that some 3-key
combinations worked while others didn’t. I spent a few hours going over
the code and looking in FAQ’s, but didn’t find anything.

Then I tried the code on another machine and it worked!! The problem was
that my crappy keyboard couldn’t detect certain 3-key sequences >:( And
it only took me 3 hours to figure that out !!!

Steve

Oh, man, it looks like that’s it. I changed the keys around and it works
great now. Looks like I’ll be looking through more junk piles for a better
keyboard! :slight_smile:

  • Mik Mifflin

Mik Mifflin wrote:

Oh, man, it looks like that’s it. I changed the keys around and it works
great now. Looks like I’ll be looking through more junk piles for a better
keyboard! :slight_smile:

This won’t help others who run your code. Most keyboards have problems
with several simultaneous keypresses. This is usually a hardware limitation;
there is no software magic SDL can do to prevent this

Modifier keys (shift, control, alt etc) are often more robust in this
regard so they make good keys for keyboard control.

Of course, if you have a choice then designing your game to not require many
simultaneous keypresses is the best solution (and probably makes the game
easier to control as well)

Don’t fall in the “my keyboard is a 102-button gamepad” trap. Look
rather at good console games for inspiration

Don’t fall in the “my keyboard is a 102-button gamepad” trap. Look
rather at good console games for inspiration

Mattias is right, and it’s also important to stress the need for
user-definable keys. Frequently this sort of thing can be avoided (or at
least, reduced), if you allow the users some way to change the keys to
their liking. Ideally, this is a nice, graphical, in-game menu, but even a
text-based config file is better than nothing.

–ryan.

Mattias Engdeg?rd wrote:

Mik Mifflin wrote:

Oh, man, it looks like that’s it. I changed the keys around and it works
great now. Looks like I’ll be looking through more junk piles for a
better
keyboard! :slight_smile:

This won’t help others who run your code. Most keyboards have problems
with several simultaneous keypresses. This is usually a hardware
limitation; there is no software magic SDL can do to prevent this

Modifier keys (shift, control, alt etc) are often more robust in this
regard so they make good keys for keyboard control.

Of course, if you have a choice then designing your game to not require
many simultaneous keypresses is the best solution (and probably makes the
game easier to control as well)

Don’t fall in the “my keyboard is a 102-button gamepad” trap. Look
rather at good console games for inspiration

Yes, I have fixed it for my current keyboard, and it’s just 3 keys. It’s
an asteroids clone. How complex could the controls get? Is there any
pattern to this “some keyboards can’t detect multiple keypresses” stuff? I
want to make sure people can play the game, becuase as it is now, you
cannot change the controls without altering the source.–

  • Mik Mifflin

Most new keyboards simply have a grid, which is used roughly as rows
and columns of keys and if you get keys from two different rows then it
will lock two other keys at the cross points. You can try if your
keyboard is like that simply press “y” and “g” and then test if “t” or
"h" work. If not then most likely your keyboard is using simple grid
detection for the basic keys.

Caps lock, Shifts, ctrls and Alts are detected as qualifier chain, but
all the else are detected as n row grid. I don’t know if those
"windows" special keys are in the grid or in the qualifier chain. If
this griding follows the layout of the keyboard then the above test
will fail. The more rows is used in grid detection the beter multiple
simultaneus key presses works. I don’t think there have ever been a
"gridless" keyboard, but the number of rows and the layout of the grids
can make huge difference.On 2002.01.13 23:47 Mik Mifflin wrote:

Mattias Engdeg?rd wrote:

Mik Mifflin wrote:

Oh, man, it looks like that’s it. I changed the keys around and it
works

great now. Looks like I’ll be looking through more junk piles for a
better
keyboard! :slight_smile:

This won’t help others who run your code. Most keyboards have
problems
with several simultaneous keypresses. This is usually a hardware
limitation; there is no software magic SDL can do to prevent this

Modifier keys (shift, control, alt etc) are often more robust in
this
regard so they make good keys for keyboard control.

Of course, if you have a choice then designing your game to not
require
many simultaneous keypresses is the best solution (and probably
makes the
game easier to control as well)

Don’t fall in the “my keyboard is a 102-button gamepad” trap. Look
rather at good console games for inspiration

Yes, I have fixed it for my current keyboard, and it’s just 3 keys.
It’s
an asteroids clone. How complex could the controls get? Is there any

pattern to this "some keyboards can’t detect multiple keypresses"
stuff? I
want to make sure people can play the game, becuase as it is now, you
cannot change the controls without altering the source.

Don’t fall in the “my keyboard is a 102-button gamepad” trap. Look
rather at good console games for inspiration

Yes, I have fixed it for my current keyboard, and it’s just 3 keys. It’s
an asteroids clone. How complex could the controls get? Is there any
pattern to this “some keyboards can’t detect multiple keypresses” stuff? I
want to make sure people can play the game, becuase as it is now, you
cannot change the controls without altering the source.

Steve Baker recently wrote a short piece on this, which is at

http://www.sjbaker.org/steve/omniv/index.html

under “Keyboards Are Evil”.

MadsOn Sun, 13 Jan 2002, Mik Mifflin wrote:


Mads Bondo Dydensborg. @Mads_Bondo_Dydensbor
Before we’d usually say, there is a deep rumbling sound of penguins. Now
it’s time to begin with: The earth is quaking, hell yeah the penguins are
here!!