Problems with SDL_PollEvent... [newbie]

Hello all,

I’m new to SDL and I’m probably looking over something terribly simple, but I would appreciate it if someone could point out what I’m doing wrong…

Here’s a simple program that I’ve compiled and ran, but I can’t get it to read any keys. I’ve tried rearranging the code various ways, but I can’t get it to notice a SDL_KEYDOWN event. It always passes over it. SDL_QUIT seems to work right though…

Hope somebody can help me, and I apologize for bugging you all.

MadCat13

int KeyPressed( SDL_keysym * whichKey )
{
if (whichKey->sym == SDLK_a)
{
printf(“You pressed the ‘A’ key.\n”);
return 0;
}
if (whichKey->sym == SDLK_b)
{
printf(“You pressed the ‘B’ key.\n”);
return 0;
}
if (whichKey->sym == SDLK_c)
{
printf(“You pressed the ‘C’ key.\n”);
return 0;
}
if (whichKey->sym == SDLK_ESCAPE)
{
printf(“Quitting…\n”);
return -1;
}
printf(“You pressed some other key.\n”);
return 0;
}

int PollKeyboard( void )
{
SDL_Event event;
int Quit = 0;

 while( SDL_PollEvent ( &event ))
 {
      switch(event.type)
      {
      case SDL_KEYDOWN:
           Quit = KeyPressed(&event.key.keysym);
           break;
      case SDL_QUIT:
           Quit = -1;
           break;
      }
 }

 return Quit;

}

int main(int argv, char ** argc)
{
int Quit = 0;
printf(“Polling keyboard…\n\n”);

 while( Quit == 0 )
 {
      Quit = PollKeyboard();
 }

 return 0;

}

int main(int argv, char ** argc)
{
int Quit = 0;
printf(“Polling keyboard…\n\n”);

 while( Quit == 0 )
 {
      Quit = PollKeyboard();
 }

 return 0;

}

  1. You never called SDL_Init(SDL_INIT_VIDEO). You’re lucky that the
    program doesn’t just crash. (The event subsystem is tied to the video
    subsystem, that’s why it’s SDL_INIT_VIDEO …). Example:

    If (SDL_Init(SDL_INIT_VIDEO) == -1)
    printf(“Failed to initialize SDL! Reason: %s\n”, SDL_GetError());

  2. You need to set a video mode to receive events. Example:

    if (SDL_SetVideoMode(640, 480, 16, 0) == NULL)
    printf(“failed to set a video mode! Reason: %s\n”, SDL_GetError());

  3. Events to the window that is created by SDL_SetVideoMode() will be what
    you get in PollKeyboard(). If some other window has the focus, for
    example, you won’t get any input in your program.

  4. Don’t forget to call SDL_Quit() before ending the program to clean up
    the video, etc.

I assume you are just experimenting with SDL, which is why you are just
polling input, but if ALL you want is a keyboard hook, you are looking in
the wrong place. This should get you going, otherwise.

–ryan.

int main(int argv, char ** argc)
{
int Quit = 0;
printf(“Polling keyboard…\n\n”);

 while( Quit == 0 )
 {
      Quit = PollKeyboard();
 }

 return 0;

}

  1. You never called SDL_Init(SDL_INIT_VIDEO). You’re lucky that the
    program doesn’t just crash. (The event subsystem is tied to the video
    subsystem, that’s why it’s SDL_INIT_VIDEO …). Example:

    If (SDL_Init(SDL_INIT_VIDEO) == -1)
    printf(“Failed to initialize SDL! Reason: %s\n”, SDL_GetError());

  2. You need to set a video mode to receive events. Example:

    if (SDL_SetVideoMode(640, 480, 16, 0) == NULL)
    printf(“failed to set a video mode! Reason: %s\n”,
    SDL_GetError());

  3. Events to the window that is created by SDL_SetVideoMode() will be what
    you get in PollKeyboard(). If some other window has the focus, for
    example, you won’t get any input in your program.

  4. Don’t forget to call SDL_Quit() before ending the program to clean up
    the video, etc.

I assume you are just experimenting with SDL, which is why you are just
polling input, but if ALL you want is a keyboard hook, you are looking in
the wrong place. This should get you going, otherwise.

–ryan.


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

I knew I had forgot something insanely simple :slight_smile:

Yeah, I’m just feeling it out.

And thanks for answering me and not laughing me out of the mailing list :slight_smile:

MadCat13

----- Original Message -----
From: icculus@icculus.org (Ryan C. Gordon)
To:
Sent: Thursday, December 13, 2001 12:16 AM
Subject: Re: [SDL] Problems with SDL_PollEvent… [newbie]