<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>al_get_keyboard_state error</title>
		<link>http://www.allegro.cc/forums/view/617425</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Thu, 31 May 2018 02:52:51 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hi, I&#39;m making this little tris game and I wanted to abilitate keyboard play, but I&#39;ve ran into an issue: basically an exception occurs on al_get_keyboard_state(keySTate).</p><p>My code is somenthing like this:</p><p>...<br />ALLEGRO_EVENT_QUEUE *event_q = NULL;<br />...<br />al_install_keyboard();<br />...<br />event_q = al_create_event_queue();<br />...<br />al_register_event_source(event_q, al_get_keyboard_event_source());<br />... (start loop)<br />ALLEGRO_EVENT *event = NULL;<br />ALLEGRO_KEYBOARD_STATE *keyState = NULL;</p><p>al_wait_for_event(event_q, event);<br />al_get_keyboard_state(keyState);</p><p>if (al_key_down(keyState, ALLEGRO_KEY_Q))<br />//do things<br />... (end loop)</p><p>What did I mess up? <img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SaSSolino)</author>
		<pubDate>Mon, 28 May 2018 21:29:49 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>That function takes a pointer that needs to point to some allocated memory. Right now your pointer points to nothing (note how you set it to NULL). The easiest way to resolve this is to allocate the state on the stack, like so:</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/ALLEGRO_KEYBOARD_STATE"><span class="a">ALLEGRO_KEYBOARD_STATE</span></a> keyState<span class="k2">;</span>
<a href="http://www.allegro.cc/manual/al_get_keyboard_state"><span class="a">al_get_keyboard_state</span></a><span class="k2">(</span><span class="k3">&amp;</span>keyState<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

There is one more issue just like that in your code, see if you can spot it.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SiegeLord)</author>
		<pubDate>Mon, 28 May 2018 23:02:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks for the reply.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SaSSolino)</author>
		<pubDate>Tue, 29 May 2018 12:37:33 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You may not want to rely on the keyboard state, but rather keyboard events. It&#39;s fairly easy to track which keys are down.</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">int</span> keys<span class="k2">[</span>ALLEGRO_KEY_MAX<span class="k2">]</span> <span class="k3">=</span> <span class="k2">{</span><span class="n">0</span><span class="k2">}</span><span class="k2">;</span>

<span class="c">//... Wait for an event first</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="k3">=</span> <span class="k1">true</span><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_UP<span class="k2">)</span> <span class="k2">{</span>keys<span class="k2">[</span>ev.keyboard.keycode <span class="k3">=</span> <span class="k1">false</span><span class="k2">;</span><span class="k2">}</span>

<span class="c">//... Monitor key array inside logic code</span>

<span class="k1">if</span> <span class="k2">(</span>keys<span class="k2">[</span>ALLEGRO_KEY_ESCAPE<span class="k2">]</span><span class="k2">)</span> <span class="k2">{</span>Quit<span class="k2">(</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>Tue, 29 May 2018 17:19:58 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>That is almost exactly what I do also.  (except my array is called &#39;key&#39; instead of keys)</p><p>I thought when I implemented that in Allegro 5, I was doing something shady, like trying to revive the way things used to be in Allegro 4.</p><p>But seeing someone else do it as well, kind of legitimizes it, in my mind....</p><p>I have a question for Edgar.</p><p>In my program, I make my int key[] array global.</p><p>(I declare it outside of any function, and extern it in my main header file)</p><p>What would be a more elegant approach to this?<br />Not from a functional point, because it works just fine, just from a &#39;better programming style&#39; point.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Michael Weiss)</author>
		<pubDate>Wed, 30 May 2018 08:12:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>A quick and dirty solution to global proliferation is functions.</p><p>Turn you key array into a <span class="source-code"><span class="k1">bool</span> Key<span class="k2">(</span><span class="k1">int</span> k<span class="k2">)</span> <span class="k2">{</span><span class="k1">return</span> keys<span class="k2">[</span>k<span class="k2">]</span><span class="k2">;</span><span class="k2">}</span></span> function. Then you can hide the keys array in a module, and expose it through your function. This reduces global proliferation.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Thu, 31 May 2018 02:52:51 +0000</pubDate>
	</item>
</rss>
