Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Keyboard input buffer.

This thread is locked; no one can reply to it. rss feed Print
Keyboard input buffer.
Rob Johnson
Member #7,226
May 2006

Hello there. I've developed a menu for a game. I'm using the keyboard to interact with it. However the input speed is far too fast. Is there a way of buffering the input without cutting the actual timer speed down. I have a background being drawn which requires my set FPS. Obviously I need to turn this buffer off once the game is actually playing. Any suggetsions or ideas would help me greatly. I'm thinking of some sort of loop but I know this would slow down the game speed.

gnolam
Member #2,030
March 2002
avatar

Depends on what you're asking.

If it's "how do I register each keypress only once?": just copy the key array to your own buffer and compare them when checking for a key press. If they're both the same, the key is being held down. If key[] is set but keybuf[] isn't, the key was just pressed. If it's the other way around, the key was just released.

char keybuf[KEY_MAX];

void check_input(void)
{
 if(key[KEY_A] && !keybuf[KEY_A])
   do_something();

 //copy the key[] array to your own buffer
 memcpy((void*)keybuf, (void*)key, sizeof(keybuf));
}

If it's "how do I slow certain things down": set a counter which you decrement each cycle. When it's zero, the action may be taken again.

#define NUMBER_OF_CYCLES 5

int cooldown_key_a = 0;

void check_input(void)
{
 if (key[KEY_A])
     if (cooldown_key_a == 0) {
         do_something();
         cooldown_key_a = NUMBER_OF_CYCLES;
        }
        
 cooldown_key_a--;
}

Code typed directly into the reply box, so YMMV.

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Rob Johnson
Member #7,226
May 2006

Ah many thanks, especially for the super fast response. I think option two would be far easier for me to implement into this setup.

Elias
Member #358
May 2000

Another option would be:

if (keypressed())
{
    k = readkey();
}

--
"Either help out or stop whining" - Evert

Go to: