Sign in to follow this  

SDL KeyRepeat

This topic is 4688 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites

This topic is 4688 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this