[SDL] keyboard events

Iain Bethune iain at pyramid-productions.net
Wed Sep 21 11:51:37 PDT 2005


It's a bug in your code.

case SDL_KEYDOWN:
            std::cout << "KEY PRESSED" << std::endl;
	break;
        case SDL_KEYUP:
            std::cout << "KEY RELEASED" << std::endl;
	break;


ALWAYS put breaks after your switch cases otherwise things happen that 
are usually difficult to trace!

- Iain


> I am a first time SDL user.  I am trying to move a sprite around and 
> notice that when I press a key and DO NOT RELEASE it I seem to get a 
> SDL_KEYDOWN event followed immediately by a SDL_KEYUP event.  Then 
> when I release the key I get another SDL_KEYUP event.
>
>  I am running Fedora Core 3 on a Toshiba laptop?  Is this a OS or 
> keyboard issie?
>
>  Here is the relevant code:
>
>  while (1)
>    {
>
>      // Poll for events, and handle the ones we care about.
>      SDL_Event event;
>      if (SDL_PollEvent(&event))
>      {
>        std::cout << "event.key.keysym.sym is: " << 
> event.key.keysym.sym << std::endl;
>       
>        switch (event.type)
>        {
>        case SDL_KEYDOWN:
>            std::cout << "KEY PRESSED" << std::endl;
>           
>        case SDL_KEYUP:
>            std::cout << "KEY RELEASED" << std::endl;
>        }
>       
>      }
>     
>    }
>
>  The output:
>
>  event.key.keysym.sym is: 100
>  KEY PRESSED
>  KEY RELEASED
>  event.key.keysym.sym is: 100
>  KEY RELEASED
>  event.key.keysym.sym is: 102
>  KEY PRESSED
>  KEY RELEASED
>  event.key.keysym.sym is: 102
>  KEY RELEASED
>
>
>  Is this normal?  I was hope to move the sprite while the key was 
> pressed down and stop it when the key was released.
> _______________________________________________
> SDL mailing list
> SDL at libsdl.org
> http://www.libsdl.org/mailman/listinfo/sdl





More information about the SDL mailing list