SDL KeyRepeat

Started by
1 comment, last by siliconsurfer 19 years, 2 months ago
Hi, I have the following piece of code /************************************************************************** while (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT) { done = 1; } if (event.type == SDL_KEYDOWN) { if (event.key.keysym.sym == SDLK_ESCAPE) { done = 1; } if(event.key.keysym.sym == SDLK_p) { musicPaused=!musicPaused; } keys = SDL_GetKeyState(NULL); if(keys[SDLK_PAGEDOWN]) { theEngine->Sound->music("setVolume", 0, theEngine->Sound->music("getVolume", 0, 0)-8); } if(keys[SDLK_PAGEUP]) { theEngine->Sound->music("setVolume", 0, theEngine->Sound->music("getVolume", 0, 0)+8); } } } \****************************************************************************/ Near the bottom of the code if PAGEDOWN or PAGEUP is pressed it calls a function to change the level of the volume. My problem is I want to be able to hold PAGEDOWN or PAGEUP to continually change the volume. I find that if I press and hold the button down it only makes once change to the level. I looked at the SDL documentation and found SDL_EnableKeyRepeat() I added it to the code just above if(keys[SDLK_PAGEDOWN]), but this didn't make any difference. Any suggestions?
Advertisement
I think your loop is kind of off.

Take a look at my comments -

while (SDL_PollEvent(&event)){   if (event.type == SDL_QUIT)    {       done = 1;    }   if (event.type == SDL_KEYDOWN)   {      // Good, check to see if escape was preseed      if (event.key.keysym.sym == SDLK_ESCAPE)       {          done = 1;       }      // Good, check to see if p was preseed      if(event.key.keysym.sym == SDLK_p)      {         musicPaused=!musicPaused;      }    }   }       keys = SDL_GetKeyState(NULL);      // Let's try the previous method         //if(keys[SDLK_PAGEDOWN])       if(event.key.keysym.sym == SDLK_PAGEDOWN)      {         theEngine->Sound->music("setVolume", 0,    theEngine->Sound->music("getVolume", 0, 0)-8);      }         if(keys[SDLK_PAGEUP])      {         theEngine->Sound->music("setVolume", 0,    theEngine->Sound->music("getVolume", 0, 0)+8);      }  }


Now see the changes I made? Your method that you were using looked like it was working fine, so try using that first. If that does notwork, you will need to do this.

while (SDL_PollEvent(&event)){   if (event.type == SDL_QUIT)    {       done = 1;    }   if (event.type == SDL_KEYDOWN)   {      // Good, check to see if escape was preseed      if (event.key.keysym.sym == SDLK_ESCAPE)       {          done = 1;       }      // Good, check to see if p was preseed      if(event.key.keysym.sym == SDLK_p)      {         musicPaused=!musicPaused;      }      // wtf mate?!?      //keys = SDL_GetKeyState(NULL);      // Let's try the previous method         //if(keys[SDLK_PAGEDOWN])       if(event.key.keysym.sym == SDLK_PAGEDOWN)      {         theEngine->Sound->music("setVolume", 0,    theEngine->Sound->music("getVolume", 0, 0)-8);      }   //      if(keys[SDLK_PAGEUP])       // Let's try the previous method      if(event.key.keysym.sym == SDLK_PAGEUP)      {         theEngine->Sound->music("setVolume", 0,    theEngine->Sound->music("getVolume", 0, 0)+8);      }   }   }


That main idea is to let everything update event-wise, then get the state of the keys and process that. I think one of those methods should do what you are trying to accompish. Just note that now, for every key press, you will be getting at least 12-15 events, so one press of PAGEUP will make the volume go up at least to 96! You will have to compensate for this.

As a side note, you should look into a more efficient main loop. I will give you a hint to look up SDL_PeepEvent. Hope this helps.

- Drew
Yes you were very right.
I moved the code, down to where I process the rest of the input, such as moving the character about the screen. This is controlled by setting a frame rate, and checking SDL_GetTicks.

This provides a nice smooth volume up and down.

Cheers

I'll take a look at SDL_PeepEvent too, thanks!

This topic is closed to new replies.

Advertisement