<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Keyboard event queue not really empty when it should be</title>
		<link>http://www.allegro.cc/forums/view/616589</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sun, 13 Nov 2016 16:30:50 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I made function that gets changes of keys from queue. Problem in this case is that when i press and hold single key that event is registered once more in next if even after al_get_next_event (which should remove that event from queue). It only happens on key DOWN, not UP.</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span><span class="k1">void</span> keyboard_receive<span class="k2">(</span><span class="k1">void</span><span class="k2">)</span> <span class="k2">{</span>
<span class="number">  2</span>  <span class="c">/* Checks if there is anything in keyboard_event_queue */</span>
<span class="number">  3</span>  <span class="k1">if</span> <span class="k2">(</span><span class="k3">!</span><a href="http://www.allegro.cc/manual/al_is_event_queue_empty"><span class="a">al_is_event_queue_empty</span></a><span class="k2">(</span>keyboard_event_queue<span class="k2">)</span><span class="k2">)</span> <span class="k2">{</span>
<span class="number">  4</span>
<span class="number">  5</span>    <span class="c">/* Get whats on queue */</span>
<span class="number">  6</span>    <a href="http://www.allegro.cc/manual/al_get_next_event"><span class="a">al_get_next_event</span></a><span class="k2">(</span>keyboard_event_queue, <span class="k3">&amp;</span>keyboard_event<span class="k2">)</span><span class="k2">;</span>
<span class="number">  7</span>
<span class="number">  8</span>    <span class="c">/* Reads event and acts accordingly */</span>
<span class="number">  9</span>    <span class="c">// some code</span>
<span class="number"> 10</span>
<span class="number"> 11</span>    <span class="c">/* Checks if there is anything left in queue - if there is, receive it */</span>
<span class="number"> 12</span>    <span class="k1">if</span> <span class="k2">(</span><span class="k3">!</span><a href="http://www.allegro.cc/manual/al_is_event_queue_empty"><span class="a">al_is_event_queue_empty</span></a><span class="k2">(</span>keyboard_event_queue<span class="k2">)</span><span class="k2">)</span> <span class="k2">{</span>
<span class="number"> 13</span>      <a href="http://www.allegro.cc/manual/al_peek_next_event"><span class="a">al_peek_next_event</span></a><span class="k2">(</span>keyboard_event_queue, <span class="k3">&amp;</span>keyboard_event<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 14</span>      <a href="http://www.delorie.com/djgpp/doc/libc/libc_624.html" target="_blank">printf</a><span class="k2">(</span><span class="s">"\n key = %i"</span>, keyboard_event.keyboard.keycode<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 15</span>      keyboard_receive<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 16</span>    <span class="k2">}</span>
<span class="number"> 17</span>  <span class="k2">}</span>
<span class="number"> 18</span><span class="k2">}</span>
</div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Aleksa)</author>
		<pubDate>Sun, 13 Nov 2016 02:10:26 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If that is your actual code, you are probably getting an ALLEGRO_EVENT_KEY_CHAR event. You need to check for the event types you are interested in, and ignore the others.</p><p><a href="http://liballeg.org/a5docs/trunk/events.html#allegro_event_key_down">http://liballeg.org/a5docs/trunk/events.html#allegro_event_key_down</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (torhu)</author>
		<pubDate>Sun, 13 Nov 2016 02:55:26 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It&#39;s mine code. I am checking for event type in &quot;//some code&quot; part. Only source that is registered to keyboard_event_queue is keyboard.</p><p>I attached whole .c file.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Aleksa)</author>
		<pubDate>Sun, 13 Nov 2016 03:07:19 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Allegro can add events to the queues at any time, it uses a separate thread for it. You have to get all events, then ignore the ones you don&#39;t care about.</p><p>If you print the type of the unexpected events, you will probably see that it is ALLEGRO_EVENT_KEY_CHAR.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (torhu)</author>
		<pubDate>Sun, 13 Nov 2016 03:23:42 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Recursion is not really called for here. Just loop over the queue&#39;s events.</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">while</span> <span class="k2">(</span><span class="k3">!</span><a href="http://www.allegro.cc/manual/al_is_event_queue_empty"><span class="a">al_is_event_queue_empty</span></a><span class="k2">(</span>queue<span class="k2">)</span><span class="k2">)</span> <span class="k2">{</span>
   <a href="http://www.allegro.cc/manual/al_get_next_event"><span class="a">al_get_next_event</span></a><span class="k2">(</span>queue , <span class="k3">&amp;</span>ev<span class="k2">)</span><span class="k2">;</span>
   <span class="k1">if</span> <span class="k2">(</span>ev.type <span class="k3">=</span><span class="k3">=</span> ALLEGRO_EVENT_KEY_DOWN<span class="k2">)</span> <span class="k2">{</span>
      keys<span class="k2">[</span>ev.keyboard.keycode<span class="k2">]</span> <span class="k3">=</span> <span class="k1">true</span><span class="k2">;</span>
   <span class="k2">}</span>
   <span class="k1">else</span> <span class="k1">if</span> <span class="k2">(</span>ev.type <span class="k3">=</span><span class="k3">=</span> ALLEGRO_EVENT_KEY_UP<span class="k2">)</span> <span class="k2">{</span>
      keys<span class="k2">[</span>ev.keyboard.keycode<span class="k2">]</span> <span class="k3">=</span> <span class="k1">false</span><span class="k2">;</span>
   <span class="k2">}</span>
<span class="k2">}</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Sun, 13 Nov 2016 03:25:15 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yeah, don&#39;t use recursion in C unless it simplifies your code significantly. There&#39;s no tail call elimination, it&#39;s slow, and you risk running out of stack space <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (torhu)</author>
		<pubDate>Sun, 13 Nov 2016 03:31:03 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I used recursion as i just wanted to make it work for now (was first thing on my mind). Implementing while loop is giving me expected results which is that system is simulating key rapid key press when key is held down. I&#39;m just not sure why first solution was not giving same result <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Aleksa)</author>
		<pubDate>Sun, 13 Nov 2016 05:26:14 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The system is not simulating key repeat, it&#39;s a basic keyboard feature that goes back decades <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /></p><p>By the way, why was using recursion the first thing on your mind? Did you learn a functional programming language first?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (torhu)</author>
		<pubDate>Sun, 13 Nov 2016 06:54:35 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Because i was like lets do this over and over until there is no stuff in queue, which should work if I call same function again. Not sure what you mean by second question. First and almost only programming language I started learning (and still trying to learn) is C, which i learn on my own from book and internet <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Aleksa)</author>
		<pubDate>Sun, 13 Nov 2016 16:30:50 +0000</pubDate>
	</item>
</rss>
