{"id":110,"date":"2008-01-01T00:00:00","date_gmt":"2008-12-01T07:00:53","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=265"},"modified":"2008-01-01T00:00:00","modified_gmt":"2008-12-01T07:00:53","slug":"keycode-variables","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=110","title":{"rendered":"KeyCode Variables"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n\/\/ create constants for all letter and number keys:\r\nvar alphabet:Array = &quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;.split(&quot;&quot;);\r\nvar nums:Array = &#x5B;&quot;ZERO&quot;,&quot;ONE&quot;,&quot;TWO&quot;,&quot;THREE&quot;,&quot;FOUR&quot;,&quot;FIVE&quot;,&quot;SIX&quot;,&quot;SEVEN&quot;,&quot;EIGHT&quot;,&quot;NINE&quot;];\r\n\r\nvar key:Object = new Object();\r\nfor (var i:int  = 0; i&lt;alphabet.length; i++)\r\n\t key&#x5B;alphabet&#x5B;i]] = 65 + i;\r\n\t \r\nfor (i = 0; i&lt;nums.length; i++){\r\n\t var code:int = 48 + i;\r\n     key&#x5B;nums&#x5B;i]]= code;\r\n\t key&#x5B;i] = code;\r\n}\r\n\t \r\n\r\n\/\/ test them out\r\nstage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);\r\nfunction onKeyPressed(evt:KeyboardEvent):void {\r\n\tif (evt.keyCode == key.A){\r\n\t\ttrace(&quot;the a was pressed&quot;);\r\n\t}\r\n\tif (evt.keyCode == key.B){\r\n\t\ttrace(&quot;the b was pressed&quot;);\r\n\t}\r\n\tif (evt.keyCode == key.NINE){\r\n\t\ttrace(&quot;the 9 key was pressed&quot;);\r\n\t}\r\n\tif (evt.keyCode == key&#x5B;&quot;0&quot;]){\r\n\t\ttrace(&quot;the 0 key was pressed&quot;);\r\n\t}\r\n}\r\n<\/pre>\n<p>This is an easy way to store values for alphanumeric keys in variables that make sense&#8230; instead of having to do things like this:<\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n\/\/ hit the zero key\r\nif (evt.keyCode == 48){\r\n   trace(&quot;the zero key was hit&quot;);\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\/\/ create constants for all letter and number keys: var alphabet:Array = &quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;.split(&quot;&quot;); var nums:Array = &#x5B;&quot;ZERO&quot;,&quot;ONE&quot;,&quot;TWO&quot;,&quot;THREE&quot;,&quot;FOUR&quot;,&quot;FIVE&quot;,&quot;SIX&quot;,&quot;SEVEN&quot;,&quot;EIGHT&quot;,&quot;NINE&quot;]; var key:Object = new Object(); for (var i:int = 0; i&lt;alphabet.length; i++) key&#x5B;alphabet&#x5B;i]] = 65 + i; for (i = 0; i&lt;nums.length; i++){ var code:int = 48 + i; key&#x5B;nums&#x5B;i]]= code; key&#x5B;i] = code; } \/\/ test them [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[75,7],"tags":[3,4,78],"class_list":["post-110","post","type-post","status-publish","format-standard","hentry","category-keys","category-object","tag-actionscript","tag-flash","tag-keys"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/110","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=110"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/110\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}