SDL Won't Recognize 3 inputs

In my program, whenever I press 3 buttons at the same time, the third one to be pressed isn't recognized by the program. It works perfectly fine if one or two buttons are pressed simeltaniously but not if 3 or more are. Does anyone know how to fix this? I've tried both regular ways of detecting input with SDL and using SDL_GetKeyState(). I have the same problem either way.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  void Player::Handle_Event(SDL_Event Event, vector<Shot> *shots)
{
    Set_CenterPosition();

    if (Event.type == SDL_KEYDOWN)
    {
        switch(Event.key.keysym.sym)
        {
        case SDLK_UP:
            Velocity.Y = -PLANE_SPEED;
            break;

        case SDLK_DOWN:
            Velocity.Y = PLANE_SPEED;
            break;

        case SDLK_LEFT:
            Velocity.X = -PLANE_SPEED;
            break;

        case SDLK_RIGHT:
            Velocity.X = PLANE_SPEED;
            break;
/*
        case SDLK_PAGEUP:
            Velocity.X = -PLANE_SPEED;
            Velocity.Y = -PLANE_SPEED;
            break;

        case SDLK_PAGEDOWN:
            Velocity.X = PLANE_SPEED;
            Velocity.Y = -PLANE_SPEED;
            break;
*/

        case SDLK_SPACE:
            if ( Clip.y == MOVING_HORIZONTALLY )
            {
                if (Clip.x == STRAIGHT_AHEAD)
                {
                    Shot newshot( Player_Shot, CenterPosition.X - 2, CenterPosition.Y - Clip.h/2 - 10, 0, -SHOT_SPEED );

                    shots->push_back(newshot);
                }
                else if (Clip.x == MOVING_LEFT)
                {
                    Shot newshot( Player_Shot, CenterPosition.X - Clip.w/2 - 10, CenterPosition.Y - 5, -SHOT_SPEED, 0 );

                    shots->push_back(newshot);
                }
                else if (Clip.x == MOVING_RIGHT)
                {
                    Shot newshot( Player_Shot, CenterPosition.X + Clip.w/2 - 10, CenterPosition.Y - 5, SHOT_SPEED, 0 );

                    shots->push_back(newshot);
                }
            }
            else if ( Clip.y == MOVING_FORWARD )
            {
                if ( Clip.x == STRAIGHT_AHEAD )
                {
                    Shot newshot( Player_Shot, CenterPosition.X - 2, CenterPosition.Y - Clip.h/2 - 10, 0, -SHOT_SPEED );

                    shots->push_back(newshot);
                }
                else if ( Clip.x == MOVING_LEFT )
                {
                    Shot newshot( Player_Shot, CenterPosition.X - Clip.w/2, CenterPosition.Y - Clip.h/2, -SHOT_SPEED, -SHOT_SPEED );

                    shots->push_back(newshot);
                }
            }
            else if (Clip.y == MOVING_BACKWARD)
            {
                if ( Clip.x == STRAIGHT_AHEAD )
                {
                    Shot newshot( Player_Shot, CenterPosition.X - 2, CenterPosition.Y + Clip.h/2, 0, SHOT_SPEED );

                    shots->push_back(newshot);
                }
            }
            //cout<<shots->size()<<"   end"<<endl;
            break;

        }
    }
    else if (Event.type == SDL_KEYUP)
    {
        switch(Event.key.keysym.sym)
        {
        case SDLK_UP:
        case SDLK_DOWN:
            Velocity.Y = 0;
            break;

        case SDLK_LEFT:
        case SDLK_RIGHT:
            Velocity.X = 0;
            break;

        case SDLK_SPACE:
            break;
        }
    }
}
bump
What happens if you press the same buttons when typing text? Does it recognise the third key? If it doesn't, it's probably a hardware limitation of your keyboard.
Last edited on
Idk. I don't type text at any point in my program. If you meant typing text in general, then it recognizes it just fine.
bump
I mean in general.

How do you know the third key isn't recognized? Maybe give an example.
If I hold the up arrow and the left arrow at the same time (My character is moving up and left) and then press space, it doesn't shoot (it's a UFO); however, if I can time it write and let go of one of the buttons and hit space before it switches directions, it shoots just fine. I also was able to shoot in that direction just fine when the only two buttons that I pressed were page up and space bar. Lastly, it has never failed when I'm only using one arrow key and then press space to shoot
Last edited on
And if you hold left arrow and space and then press up, will it move up?
Yeah I get it this is a general "problem" in some keyboards like mine. And when I read about it, I found out that this kind of stuff comes up when you press a number of keys from the same region, the system beeps(not on all systems) and the keyboard "misbehaves" so this is basically not your fault.

Thanks,
Aceix.
@Peter
It won't move up. Other combinations of arrow keys did switch directions but stopped shooting

@Aceix
Thanks for the explanation. Do you know if there is any way around this or am I just gonna have to deal with it?
It's not a software problem. It's a limitation of the keyboard.

There's nothing you can do as a programmer. The only "solutions" are:

1) Get a better keyboard. Keyboards designed for "gaming" often allow for more keys to be pressed simultaneously... which is one reason why they typically cost significantly more than other keyboards.

2) Use a gamepad instead of a keyboard

3) Remap your keys to buttons that are more likely to work when pressed simulateously (varies by keyboard, is largely trial and error)
Last edited on
Oh ok. I like your second suggestion as a solution but I don't currently own a gamepad or joystick (or at least I can't find mine) so I'm going to use a PS3 controller instead. I'm writing a small separate program to get used to using a joystick before I try to implement this in my main project but am having some difficulty. The program is recognizing the controller but not any events.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <SDL/SDL.h>
#include <iostream>

using namespace std;

SDL_Surface* screen = NULL;

SDL_Joystick* Controller = NULL;

SDL_Event Event;

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

bool done = false;

int main ( int argc, char** argv )
{
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        exit(1);
    }
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );

    if( screen == NULL )
    {
        exit(2);
    }

    if( SDL_NumJoysticks() < 1 )
    {
        exit(3);
    }

    SDL_JoystickEventState(SDL_ENABLE);
    Controller = SDL_JoystickOpen( 0 );

    cout<<"Axes: "<<SDL_JoystickNumAxes(Controller)<<endl
    <<"TrackBalls: "<<SDL_JoystickNumBalls(Controller)<<endl
    <<"Buttons: "<<SDL_JoystickNumButtons(Controller)<<endl
    <<"Hats: "<<SDL_JoystickNumHats(Controller)<<endl;
    cout<<endl;

    while (!done)
    {
        while ( SDL_PollEvent(&Event) )
        {
            switch(Event.type)
            {
            case SDL_QUIT:
                exit(0);
                break;

            case SDL_JOYBUTTONDOWN:
                cout<<"Button: "<<Event.jbutton.button<<endl;
                break;

            case SDL_JOYAXISMOTION:
                cout<<"Axis: "<<Event.jaxis.axis<<endl<<"Value: "<<Event.jaxis.value<<endl;
                break;


            }
        }
    }
}


The controller has 4 axes, 0 hats, 0 balls, and 19 buttons according to the data I've collected from this program but I can't identify what each button on the controller is labelled because it's not recognizing my input. This is my first time using any input other than a keyboard so I'm assuming that I messed up on the code.
Last edited on
The code works for me, except I have to cast Event.jbutton.button and Event.jaxis.axis to int to avoid that they are printed as char.

It seems like SDL has a problem with some PS3 controllers. http://forums.libsdl.org/viewtopic.php?t=7961
So basically, I have terrible luck with everything?
Topic archived. No new replies allowed.