Sign in to follow this  
Chris Oden

Is there a conventional way to get key input?

Recommended Posts

I'm having a little problem with my key input, I need a delay before the game registers the key again so my tetris blocks doesn't spin themselves dizzy. I've tried with brute force but that only resulted in the game also skipping key presses when I didn't want it to. I'm going to experiment a bit but my thought was is there a conventional way to do it?

Share this post


Link to post
Share on other sites
Quote:
Original post by Chris Oden
I'm having a little problem with my key input, I need a delay before the game registers the key again so my tetris blocks doesn't spin themselves dizzy. I've tried with brute force but that only resulted in the game also skipping key presses when I didn't want it to.

I'm going to experiment a bit but my thought was is there a conventional way to do it?
What API are using for keyboard input?

Share this post


Link to post
Share on other sites
Depending on what API you are using, it might be possible and desirable to watch for other key state transitions. For example, in Win32 you could watch for the WM_KEYDOWN/WM_KEYUP messages, which are sent only when the key moves from one state to the other. This would allow a block to spin as fast as the button could be pressed, but would not spin wildly when held down.

Another way is to timestamp each keypress on a per-key basis. Then, if not enough time has elapsed, you simply reject each additional keypress. When enough time has elapsed between keypresses, you accept that keypress, update the timestamp to reflect the accepted keypress, and process that input as usual. This method gives a great deal of flexibility in how you can define the input to behave as well as being independent of game cycles.

Share this post


Link to post
Share on other sites
Heh, just fix it!

I kid.

I'm using SDL in a win32 API, and as it is now I poll events in the main loop and then check what that event is in every class that needs it.

It only runs the movement if the event == SDL_KEYDOWN, I though it would make it run only once when the key was pressed but it runs continually.

Share this post


Link to post
Share on other sites
Quote:
Original post by Chris Oden
I'm using SDL in a win32 API, and as it is now I poll events in the main loop and then check what that event is in every class that needs it.
SDL has functions for enabling or disabling key repeats and for adjusting the parameters (delay and time interval) thereof.

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