Is there a conventional way to get key input?

Started by
5 comments, last by Chris Oden 16 years, 3 months ago
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?
Advertisement
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?
For that matter, what programming language are you using and what OS(s) are you targeting?
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.

throw table_exception("(? ???)? ? ???");

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.
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.
Woah, then I'll dive into the documentation once again to see if I find any treasure.

Thanks for the replies and a happy new year!

This topic is closed to new replies.

Advertisement