I came across an issue with SDL – having both w a s d and arrow keys simultaneously pressed – there’s some kind of incompatibility whilst holding down s, d and left and down as well as w d and up and left keys. If i tend to use the combinations mentioned above, only 3 keys seem to work, otherwise, any other combination with more than 3 keys works rather well. Code below:
Event checking ( in class CEvent, calling child’s functions )
switch(Event->type) {
case SDL_KEYDOWN: {//KEYBOARD_KEYDOWN
onKeyDown(Event->key.keysym.sym,Event->key.keysym.mod,Event->key.keysym.unicode);
break;
}
case SDL_KEYUP: {//KEYBOARD_KEYUP
onKeyUp(Event->key.keysym.sym,Event->key.keysym.mod,Event->key.keysym.unicode);
break;
}
}
This calls two functions, overridden from parent class ( in class Core, child of CEvent ):
void Core::onKeyDown(SDLKey sym, SDLMod mod, Uint16 unicode) {
switch(sym) {
//pl1
case SDLK_UP: pl1.pmu = true; break;
case SDLK_RIGHT: pl1.pmr = true; break;
case SDLK_DOWN: pl1.pmd = true; break;
case SDLK_LEFT: pl1.pml = true; break;
//pl2
case 'w': pl2.pmu = true; break;
case 'd': pl2.pmr = true; break;
case 's': pl2.pmd = true; break;
case 'a': pl2.pml = true; break;
case SDLK_ESCAPE: onExit(); break;
}
}
void Core::onKeyUp(SDLKey sym, SDLMod mod, Uint16 unicode) {
switch(sym) {
//pl1
case SDLK_UP: pl1.pmu = false; break;
case SDLK_RIGHT: pl1.pmd = false; break;
case SDLK_DOWN: pl1.pml = false; break;
case SDLK_LEFT: pl1.pmr = false; break;
//pl2
case 'w': pl2.pmu = false; break;
case 'd': pl2.pmr = false; break;
case 's': pl2.pmd = false; break;
case 'a': pl2.pml = false; break;
}
}
Here’s how this is called( in class Core, child of CEvent ):
while( Running ) {
FPS_START = SDL_GetTicks();
while( SDL_PollEvent( &Eventhn ) ) {
onEvent( &Eventhn );
}
onLoop();
onRender();
regulateFPS();
}
onCleanup();
I just can’t figure what causes this as the program loops through this until there are no events remaining in the buffer.
This could be the key blocking phenomenon described here: http://en.wikipedia.org/wiki/Rollover_(key)#Key_blocking_and_ghosting
There’s nothing you can do about this. If you change keyboard, you may get lucky and find one that has problems with a different combination of keys (hopefully one that you’re not using).