Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [A5] Clearing Keyboard Buffer/Event Queue

Credits go to Thomas Fjellstrom for helping out!
This thread is locked; no one can reply to it. rss feed Print
[A5] Clearing Keyboard Buffer/Event Queue
Neil Roy
Member #2,229
April 2002
avatar

I have been working on a game using Allegro 5, and so far I love it. When playing my game, and my character dies and runs out of lives, the game displays a GAME OVER message then goes back to the main title screen which awaits the player to press any key to continue.

The problem is that sometimes it will display the title screen and then quickly skip past it because the player was holding down an arrow key when they died. Even though they let go of the key before a title screen is displayed, it still seems to be buffered, or in the event queue. A similar problem happens once in a blue moon at start up. The character will show up as already moved off their starting spot, I think due to the same problem.

I want to know is there a way I can clear the keyboard event queue? I have inserted code to wait in a loop if while a key is being pressed, but this will cause possible delays and seems like a hack when clearing the queue/buffer would be a more eloquent solution.

This is what I have been inserting so far...

   do {
      al_wait_for_event(event_queue, &event);
   } while(event.type == ALLEGRO_EVENT_KEY_DOWN);

Any ideas? Or am I stuck with the above code?

---
“I love you too.” - last words of Wanda Roy

Thomas Fjellstrom
Member #476
June 2000
avatar

You can just drain the entire queue ( al_flush_event_queue ). You probably don't care one bit about the events queued up once you hit the game over screen.

also you don't get repeated key down events if a key is held. you get one when the key is pressed, and then a key up event when it's released. you get KEY_CHAR for repeats IIRC.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Neil Roy
Member #2,229
April 2002
avatar

Thanks for that, I'll give it a shot.
This should be much cleaner and there will be no chance the game will hang if a key is held down or whatever.

---
“I love you too.” - last words of Wanda Roy

Dizzy Egg
Member #10,824
March 2009
avatar

You probably want to ignore the release events if you're in the 'game over' state. My guess is they're holding a key, then releasing it on game over triggers the code in the release event part; as Thomas said, holding down a key will only trigger an event once, when it's first pressed, it wont keep oscillating.

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

Neil Roy
Member #2,229
April 2002
avatar

Ah, okay, that's good to know. I wasn't certain if it would queue up several keys or what.

---
“I love you too.” - last words of Wanda Roy

Thomas Fjellstrom
Member #476
June 2000
avatar

This is the nice thing about events. You get one DOWN even for a key going down, and one event for a key coming UP. No more, no less. no more having to guess at it with key[].

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Go to: