Sign in to follow this  
sjoerd222

SDL keyevent

Recommended Posts

Hi I want that my player moves when a Key is pressed. But he makes only one step when I press the Key (He should go on moving till I realese the key). This should be very easy to fix but I do not get it. Could somebody help me Thank PS: I'm new to this Forum, if I made something wrong SORRY [QUOTE] while ( !end ) { // handle the events in the queue while ( SDL_PollEvent( &event ) ) { switch( event.type ) { case SDL_KEYDOWN: handleKeyPress(&event.key.keysym ); break; case SDL_QUIT: end = TRUE; break; default: break; } } if ( runlevel=4 ) draw_GL( ); } quit(); }[/QUOTE]

Share this post


Link to post
Share on other sites
A quick fix would be to call:

SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );

In your initialization code. This will cause SDL to keep sending your app the Key Down events when a user holds a key.

Probably a better fix would involve setting the object's velocity rather than just incrementing its position (which I assume is what you're doing).

Hope that helps.

Share this post


Link to post
Share on other sites
I'm changing the position of a camera in OpenGL. If I would have something like velocity, the application would allways try to move the camera with velocity=0. That would use more Power than my solution I think.

Thank you!!! it will help

Share this post


Link to post
Share on other sites
"Best" solution I think is to work with gametime passed, a velocity and keystates.

On keydown you put this keystate on "pressed", so you don't always have to resend this item.

When the keystate is not pressed, you do nothing, which is fairly fast enough I guess, what you would do for moving forward is e.g. something like this:

if(forward_key_pressed)
{
translate(VelocityVector*passed_time_since_previous_frame);
}

offcourse, in order to let it work decently you need a clear system that manages frames and such.

The repeat is good for a quick fix, but isn't the nicest solution.

Share this post


Link to post
Share on other sites

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