<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>[A5] Clearing Keyboard Buffer/Event Queue</title>
		<link>http://www.allegro.cc/forums/view/612299</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Thu, 28 Mar 2013 20:20:37 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>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.</p><p>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.</p><p>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.</p><p>This is what I have been inserting so far...
</p><div class="source-code snippet"><div class="inner"><pre>   <span class="k1">do</span> <span class="k2">{</span>
      <a href="http://www.allegro.cc/manual/al_wait_for_event"><span class="a">al_wait_for_event</span></a><span class="k2">(</span>event_queue, <span class="k3">&amp;</span>event<span class="k2">)</span><span class="k2">;</span>
   <span class="k2">}</span> <span class="k1">while</span><span class="k2">(</span>event.type <span class="k3">=</span><span class="k3">=</span> ALLEGRO_EVENT_KEY_DOWN<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

Any ideas? Or am I stuck with the above code?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Roy)</author>
		<pubDate>Wed, 27 Mar 2013 23:36:37 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You can just drain the entire queue ( <span class="source-code"><a href="http://www.allegro.cc/manual/al_flush_event_queue"><span class="a">al_flush_event_queue</span></a></span> ). You probably don&#39;t care one bit about the events queued up once you hit the game over screen.</p><p>also you don&#39;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&#39;s released. you get KEY_CHAR for repeats IIRC.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Thu, 28 Mar 2013 00:05:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks for that, I&#39;ll give it a shot.<br />This should be much cleaner and there will be no chance the game will hang if a key is held down or whatever.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Roy)</author>
		<pubDate>Thu, 28 Mar 2013 01:36:10 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You probably want to ignore the release events if you&#39;re in the &#39;game over&#39; state. My guess is they&#39;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&#39;s first pressed, it wont keep oscillating.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Dizzy Egg)</author>
		<pubDate>Thu, 28 Mar 2013 16:52:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Ah, okay, that&#39;s good to know.  I wasn&#39;t certain if it would queue up several keys or what.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Roy)</author>
		<pubDate>Thu, 28 Mar 2013 20:07:26 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>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 <span class="source-code"><a href="http://www.allegro.cc/manual/key"><span class="a">key</span></a><span class="k2">[</span><span class="k2">]</span></span>.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Thu, 28 Mar 2013 20:20:37 +0000</pubDate>
	</item>
</rss>
