xiedidan / SdlOsc

Oscilloscope Client based on SDL & OpenGL
Apache License 2.0
1 stars 1 forks source link

SDL input handler #1

Open xiedidan opened 8 years ago

xiedidan commented 8 years ago

handler for mouse and keyboard events

xiedidan commented 8 years ago

For input events, two handling methods are available:

  1. polling events:
        if( SDL_PollEvent( &event ) )
        {
            if( event.type == SDL_KEYDOWN )
            {
                switch( event.key.keysym.sym )
                {
                    case SDLK_UP: ... break;
                    case SDLK_DOWN: ... break;
                    case SDLK_LEFT: ... break;
                    case SDLK_RIGHT: ... break;
                }
            }

            // if window's close button was clicked
            else if( event.type == SDL_QUIT )
            {
                quit = true;
            }
        }

This method is straightforward, but it's hard to detect key combination.

  1. key / mouse state:
        Uint8 *keystates = SDL_GetKeyState( NULL );

        if( keystates[ SDLK_UP ] )
            ...
        if( keystates[ SDLK_DOWN ] )
            ...
        if( keystates[ SDLK_LEFT ] )
            ...
        if( keystates[ SDLK_RIGHT ] )
            ...

This is much easier for key combination detection.