Massive amount of KEYDOWN events for SDLK_x in OS X?

Hello.

I am having a very strange issue that has me puzzled. I’ve built a rotating
carousel that is controlled with the z and x keys.

                case SDLK_z:
                    rotator->direction = ROTATOR_DIRECTION_PREVIOUS;
                    Rotator_MoveRotator(rotator, 1);
                    break;

                case SDLK_x:
                    rotator->direction = ROTATOR_DIRECTION_NEXT;
                    Rotator_MoveRotator(rotator, 1);
                    break;

When I launch my app and press and hold z, I get a reasonable amount of
KEYDOWN events.

If I then press and hold x, I get a massive amount of KEYDOWN events
causing the carousel to go crazy.

I’ve spent several hours debugging trying to rule out a coding error.

For the heck of it, I decided to switch SDLK_x to SDLK_SLASH. The carousel
then works properly every time.

I decided to play around some more, and noticed a pattern. If I launch the
app, press and hold x, I get a tremendous amount of KEYDOWN events. If I
then switch focus to another window, and then return back to the app
window, and press and hold x, I start getting a reasonable amount of
KEYDOWN events.

Has anyone ever seen this behavior before or am I crazy?

I’m using SDL 2 in OS X 10.8.

Thanks,
– Jason

Seems like weird behaviour in SDL, but for your problem you should not be rotating based on how quick your program gets key events. Instead either set a flag on key down and unset on key up or poll the keyboard state directly but only rotate once per x seconds, for this you could either multiply a rotational speed by the time delta between frames or fix your time step to something constant.Sent from my iPhone

On 2013-10-10, at 7:00 PM, Jason Millard wrote:

Hello.

I am having a very strange issue that has me puzzled. I’ve built a rotating carousel that is controlled with the z and x keys.

                case SDLK_z:
                    rotator->direction = ROTATOR_DIRECTION_PREVIOUS;
                    Rotator_MoveRotator(rotator, 1);
                    break;
                    
                case SDLK_x:
                    rotator->direction = ROTATOR_DIRECTION_NEXT;
                    Rotator_MoveRotator(rotator, 1);
                    break;

When I launch my app and press and hold z, I get a reasonable amount of KEYDOWN events.

If I then press and hold x, I get a massive amount of KEYDOWN events causing the carousel to go crazy.

I’ve spent several hours debugging trying to rule out a coding error.

For the heck of it, I decided to switch SDLK_x to SDLK_SLASH. The carousel then works properly every time.

I decided to play around some more, and noticed a pattern. If I launch the app, press and hold x, I get a tremendous amount of KEYDOWN events. If I then switch focus to another window, and then return back to the app window, and press and hold x, I start getting a reasonable amount of KEYDOWN events.

Has anyone ever seen this behavior before or am I crazy?

I’m using SDL 2 in OS X 10.8.

Thanks,
– Jason


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Thanks Mike for the suggestions. I do have all the rotation and speed stuff
figured out and working properly.

As I said earlier, if I change

                case SDLK_x:
                    rotator->direction = ROTATOR_DIRECTION_NEXT;
                    Rotator_MoveRotator(rotator, 1);
                    break;

to

                case SDLK_SLASH:
                    rotator->direction = ROTATOR_DIRECTION_NEXT;
                    Rotator_MoveRotator(rotator, 1);
                    break;

or switch focus to another application and then back, every thing works
properly.

So I’m convinced it’s more an OS related issue.On Thu, Oct 10, 2013 at 10:00 PM, Jason Millard <@Jason_Millard> wrote:

Hello.

I am having a very strange issue that has me puzzled. I’ve built a
rotating carousel that is controlled with the z and x keys.

                case SDLK_z:
                    rotator->direction = ROTATOR_DIRECTION_PREVIOUS;
                    Rotator_MoveRotator(rotator, 1);
                    break;

                case SDLK_x:
                    rotator->direction = ROTATOR_DIRECTION_NEXT;
                    Rotator_MoveRotator(rotator, 1);
                    break;

When I launch my app and press and hold z, I get a reasonable amount of
KEYDOWN events.

If I then press and hold x, I get a massive amount of KEYDOWN events
causing the carousel to go crazy.

I’ve spent several hours debugging trying to rule out a coding error.

For the heck of it, I decided to switch SDLK_x to SDLK_SLASH. The carousel
then works properly every time.

I decided to play around some more, and noticed a pattern. If I launch the
app, press and hold x, I get a tremendous amount of KEYDOWN events. If I
then switch focus to another window, and then return back to the app
window, and press and hold x, I start getting a reasonable amount of
KEYDOWN events.

Has anyone ever seen this behavior before or am I crazy?

I’m using SDL 2 in OS X 10.8.

Thanks,
– Jason

Here’s some numbers in a simple loop:

int z_down = 0;
int x_down = 0;
int slash_down = 0;

while(!terminate) {
    SDL_Event event;
    SDL_PollEvent(&event);

    switch (event.type) {
        case SDL_KEYDOWN:
            switch (event.key.keysym.sym) {
                case SDLK_z:
                    z_down++;
                    break;

                case SDLK_x:
                    x_down++;
                    break;

                case SDLK_SLASH:
                    slash_down++;
                    break;

                default:
                    break;
            }
            break;

        case SDL_KEYUP:
            switch (event.key.keysym.sym) {
                case SDLK_z:
                    if (z_down > 0) {
                        printf("Z Key Up: %d\n", z_down);
                        z_down = 0;
                    }
                    break;

                case SDLK_x:
                    if (x_down > 0) {
                        printf("X Key Up: %d\n", x_down);
                        x_down = 0;
                    }
                    break;

                case SDLK_SLASH:
                    if (slash_down > 0) {
                        printf("SLASH Key Up: %d\n", slash_down);
                        slash_down = 0;
                    }
                    break;

                default:
                    break;
            }
            break;
    }

Holding z down for 5 seconds, followed by x for 5 seconds, followed by
slash for 5 seconds:

Z Key Up: 28
X Key Up: 7516
SLASH Key Up: 26

Switch app focus, return back, repeat:

Z Key Up: 26
X Key Up: 25
SLASH Key Up: 23On Fri, Oct 11, 2013 at 8:09 AM, Jason Millard <@Jason_Millard> wrote:

Thanks Mike for the suggestions. I do have all the rotation and speed
stuff figured out and working properly.

As I said earlier, if I change

                case SDLK_x:
                    rotator->direction = ROTATOR_DIRECTION_NEXT;
                    Rotator_MoveRotator(rotator, 1);
                    break;

to

                case SDLK_SLASH:
                    rotator->direction = ROTATOR_DIRECTION_NEXT;
                    Rotator_MoveRotator(rotator, 1);
                    break;

or switch focus to another application and then back, every thing works
properly.

So I’m convinced it’s more an OS related issue.

On Thu, Oct 10, 2013 at 10:00 PM, Jason Millard <@Jason_Millard> wrote:

Hello.

I am having a very strange issue that has me puzzled. I’ve built a
rotating carousel that is controlled with the z and x keys.

                case SDLK_z:
                    rotator->direction = ROTATOR_DIRECTION_PREVIOUS;
                    Rotator_MoveRotator(rotator, 1);
                    break;

                case SDLK_x:
                    rotator->direction = ROTATOR_DIRECTION_NEXT;
                    Rotator_MoveRotator(rotator, 1);
                    break;

When I launch my app and press and hold z, I get a reasonable amount of
KEYDOWN events.

If I then press and hold x, I get a massive amount of KEYDOWN events
causing the carousel to go crazy.

I’ve spent several hours debugging trying to rule out a coding error.

For the heck of it, I decided to switch SDLK_x to SDLK_SLASH. The
carousel then works properly every time.

I decided to play around some more, and noticed a pattern. If I launch
the app, press and hold x, I get a tremendous amount of KEYDOWN events. If
I then switch focus to another window, and then return back to the app
window, and press and hold x, I start getting a reasonable amount of
KEYDOWN events.

Has anyone ever seen this behavior before or am I crazy?

I’m using SDL 2 in OS X 10.8.

Thanks,
– Jason