The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
one keyboard event creates two events in the program
bilsch01


Joined: 26 Feb 2015
Posts: 55
Location: Seattle, WA, USA
See the code example below. Both the 'if sflag' statement and the 'else' statement get executed for a single hit of the s-key. The output of the printf() statements is visible after the program times out and ends (about 10 seconds) - the terminal screen is then displayed. It seems like the key hit generates a program event for each of the down and up strokes - though I specified the KEY_DOWN event only. How do I code it to generate only the key down event.? Thanks.

Code:

#include <SDL2/SDL.h>
#include <stdio.h>

int main(){

int sflag=1, lcount=0;

SDL_Init(SDL_INIT_VIDEO);
SDL_DisplayMode vmode;
SDL_GetDisplayMode(0, 0, &vmode);
SDL_Window *win1 = NULL;
win1=SDL_CreateWindow("graf",0,0,vmode.w,vmode.h,SDL_WINDOW_SHOWN);
SDL_Renderer* ren = NULL;
ren =  SDL_CreateRenderer( win1, 0, SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawColor( ren, 0, 0, 0, 255 );   
SDL_RenderClear( ren );
SDL_Event event;

   
while(1)
{   if(SDL_PollEvent(&event) != 0 )     //SDL_PE = 1 if key hit
   {   const Uint8 *state = SDL_GetKeyboardState(NULL);
      const Uint8 *type  = SDL_GetKeyboardState(NULL);

      if (state[SDL_SCANCODE_S] && type[SDL_KEYDOWN])
      {   
         if (sflag==1)            
         {   
            sflag=0;
            printf("transition from 1 to 0\n");
         }
         else
         {   
            sflag=1;
            printf("transition from 0 to 1\n");
         }
      }
   }

//   SDL_SetRenderDrawColor(ren,255,0,144,255); 
//   SDL_RenderDrawPoint(ren,10,10);         //plot one pixel
//   SDL_RenderPresent(ren);

   SDL_Delay(10);
   lcount++;
   if (lcount == 1000)
   goto x100;
}

x100:;
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win1);
SDL_Quit();
return 0;
}
ChliHug


Joined: 05 May 2016
Posts: 7
You're calling SDL_GetKeyboardState twice and then check an entry in the state table with SDL_KEYDOWN. This will not have the desired effect because SDL_KEYDOWN is a event type and not a scancode. You want to compare SDL_KEYDOWN to event.type instead. Depending if you want key repeats or not, you can exclude them here too. It's probably also a good idea to loop over all events. The event queue can fill up pretty quick if you just pop one event every 10 ms.

Code:
    while (SDL_PollEvent(&event) != 0)
    { 
        const Uint8 *state = SDL_GetKeyboardState(NULL);
        const Uint32 type  = event.type;

        if (state[SDL_SCANCODE_S] && type == SDL_KEYDOWN && !event.key.repeat)
one keyboard event creates two events in the program
Rainer Deyke
Guest

On 10.06.2016 08:11, bilsch01 wrote:
Quote:
const Uint8 *type = SDL_GetKeyboardState(NULL);

if (state[SDL_SCANCODE_S] && type[SDL_KEYDOWN])

These lines are clearly incorrect. If you want to check for event type,
you're going to look at the actual event object. The second line should
look something like this:

if (e.type == SDL_KEYDOWN && e.key.keysym.scancode == SDL_SCANCODE_S)

...and the first line shouldn't exist at all.



--
Rainer Deyke

_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
one keyboard event creates two events in the program
Rainer Deyke
Guest

On 10.06.2016 13:26, Rainer Deyke wrote:
Quote:
if (e.type == SDL_KEYDOWN && e.key.keysym.scancode == SDL_SCANCODE_S)

...where 'e' is the event object, which you called 'event', so the
actual code would be:

if (event.type == SDL_KEYDOWN
&& event.key.keysym.scancode == SDL_SCANCODE_S)


--
Rainer Deyke

_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org