Hello there.
This is my first time in the forums, so greetings!
I’m having some trouble handling inputs with SDL2 and I hope someone can help me figure it out.
I’m writing a cross-platform application in C and handle the keyboard with input event. Now, I know events are triggered by a key press, key release or an autorepeat keydown and that there should be only ONE keyup event per physical release of key. I’m also aware of the ‘repeat’ field which is set to non-zero when a keydown event was triggered by an autorepeat. The real issue comes right after that, apparently, my system is generating keydowns AND keyups by autorepeat, effectively resetting the ‘repeat’ field to zero and rendering it useless. For my application, it doesn’t really matter if a key is being held down at all, however, since I also get keyups by repetition, it’s impossible to differentiate between physical and automatic key presses and release.
I’m currently running 64bit Archlinux, I just tried the same code in a different computer with 32bit Windows 7 netbook and wasn’t able to reproduce the problem, so I’m not sure whether to think it’s some kind of issue with archlinux, linux in general or just my computer.
Just in case, something as simple as this will cause the issue for me:
Code:
#include <stdio.h>
#include <SDL2/SDL.h>
int main(void)
{
SDL_Event event;
int quit = 0;
SDL_Init(SDL_INIT_VIDEO);
SDL_CreateWindow("caption", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
while (!quit)
{
while (!quit && SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYUP:
if (!event.key.repeat)
printf("KEYUP\n");
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_q)
{
quit = 1;
break;
}
if (!event.key.repeat)
printf("KEYDOWN\n");
break;
}
}
}
SDL_Quit();
return 0;
}
If I held the ‘a’ key down, the output I’d get would be:
KEYDOWN
KEYUP
KEYDOWN
KEYUP
KEYDOWN
KEYUP
…
Thanks in advance for your help!