{"id":108,"date":"2008-11-29T00:00:48","date_gmt":"2008-11-29T07:00:48","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=247"},"modified":"2008-11-29T00:00:48","modified_gmt":"2008-11-29T07:00:48","slug":"multiple-keys","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=108","title":{"rendered":"Multiple Keys"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nvar key:Object = new Object();\r\nstage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);\r\nstage.addEventListener(KeyboardEvent.KEY_UP, onKeyReleased);\r\nfunction onKeyPressed(evt:KeyboardEvent):void {\r\n\tkey&#x5B;evt.keyCode] = true;\r\n\tkey.keyCode = evt.keyCode;\r\n}\r\nfunction onKeyReleased(evt:KeyboardEvent):void {\r\n\tkey&#x5B;evt.keyCode] = false\r\n}\r\n\r\n\/\/ example\r\n\r\naddEventListener(Event.ENTER_FRAME, onLoop);\r\nfunction onLoop(evt:Event):void {\r\n\t\r\n\t\/\/trace(key.keyCode);\r\n\t \r\n\tif (key&#x5B;Keyboard.LEFT]){\r\n\t\ttrace(&quot;left&quot;);\r\n\t}\r\n\t\r\n\tif (key&#x5B;Keyboard.RIGHT]){\r\n\t\ttrace(&quot;right&quot;);\r\n\t}\r\n\t\r\n\t\/\/ keys #1, #2 and #3 are down\r\n\tif (key&#x5B;49] &amp;&amp; key&#x5B;50] &amp;&amp; key&#x5B;51]){\r\n\t\ttrace(&quot;one two thee&quot;);\r\n\t}\r\n\t\r\n\t\/\/ keys #6, #7, #8 and #9 keys are down\r\n\tif (key&#x5B;54] &amp;&amp; key&#x5B;55] &amp;&amp; key&#x5B;56] &amp;&amp; key&#x5B;57]){\r\n\t\ttrace(&quot;six seven eight nine&quot;);\r\n\t}\r\n}\r\n<\/pre>\n<p>The first 10 lines of code make up this snippet. This is an easy way to keep track of multiple key presses. For games, this is the only key technique I use &#8230; wrapped up in a Singleton.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>var key:Object = new Object(); stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed); stage.addEventListener(KeyboardEvent.KEY_UP, onKeyReleased); function onKeyPressed(evt:KeyboardEvent):void { key&#x5B;evt.keyCode] = true; key.keyCode = evt.keyCode; } function onKeyReleased(evt:KeyboardEvent):void { key&#x5B;evt.keyCode] = false } \/\/ example addEventListener(Event.ENTER_FRAME, onLoop); function onLoop(evt:Event):void { \/\/trace(key.keyCode); if (key&#x5B;Keyboard.LEFT]){ trace(&quot;left&quot;); } if (key&#x5B;Keyboard.RIGHT]){ trace(&quot;right&quot;); } \/\/ keys #1, #2 and #3 are down if (key&#x5B;49] &amp;&amp; key&#x5B;50] &amp;&amp; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,75],"tags":[3,4],"class_list":["post-108","post","type-post","status-publish","format-standard","hentry","category-associative-arrays","category-keys","tag-actionscript","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/108","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=108"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/108\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}