Created
September 29, 2015 00:26
-
-
Save girasquid/dcf7b0f05fcd3930d37f to your computer and use it in GitHub Desktop.
Revisions
-
girasquid revised this gist
Sep 29, 2015 . No changes.There are no files selected for viewing
-
girasquid created this gist
Sep 29, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,94 @@ package controllers { import com.junkbyte.console.ConsoleChannel; import starling.display.Stage; import starling.events.KeyboardEvent; public class Keyboard { private static const channel:ConsoleChannel = new ConsoleChannel("Keyboard"); private static var stage:Stage; private static var trackingKeys:Object = {}; private static var event:KeyboardEvent; public static function monitor(s:Stage):void { stage = s; stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownListener); stage.addEventListener(KeyboardEvent.KEY_UP, keyUpListener); } private static function keyDownListener(e:KeyboardEvent):void { var letter:KeyboardLetter = instantiateKey(e); letter.change(e); } private static function keyUpListener(e:KeyboardEvent):void { var letter:KeyboardLetter = instantiateKey(e); letter.change(e); } private static function instantiateKey(e:KeyboardEvent):KeyboardLetter { if(!trackingKeys.hasOwnProperty(e.keyCode)) { trackingKeys[e.keyCode] = new KeyboardLetter(); } return trackingKeys[e.keyCode]; } public static function pressing(keyCode:uint):Boolean { if(trackingKeys.hasOwnProperty(keyCode)) { return trackingKeys[keyCode].pressing(); } return false; } public static function consume(keyCode:uint):void { if(trackingKeys.hasOwnProperty(keyCode)) { trackingKeys[keyCode].consume(); } } public static function value(keyCode:uint):Number { if(trackingKeys.hasOwnProperty(keyCode)) { return trackingKeys[keyCode].value; } return 0; } } } import com.junkbyte.console.ConsoleChannel; import starling.events.KeyboardEvent; class KeyboardLetter { public var value:Number; private static const channel:ConsoleChannel = new ConsoleChannel("KeyboardLetter"); private static const DOWN_VALUE:Number = 1; private static const UP_VALUE:Number = 0; private var pressed:Boolean; public function KeyboardLetter():void { pressed = false; } public function consume():void { value = UP_VALUE; } public function pressing():Boolean { return value > UP_VALUE; } public function change(e:KeyboardEvent):void { if(e.type == KeyboardEvent.KEY_DOWN && !pressed) { value = DOWN_VALUE; pressed = true; channel.info("Changing for", e.keyCode, e.type); } else if(e.type == KeyboardEvent.KEY_UP && pressed) { value = UP_VALUE; channel.info("Changing for", e.keyCode, e.type); pressed = false; } } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ import controllers.Keyboard; import flash.ui.Keyboard; // inside a DisplayObjectContainer that is on the stage right now controllers.Keyboard.monitor(this.stage); private function loop(e:EnterFrameEvent):void { if(controllers.Keyboard.pressing(flash.ui.Keyboard.UP)) { trace("Pushing the UP button! This will log on every frame until the button isn't pressed anymore."); } if(controllers.Keyboard.pressing(flash.ui.Keyboard.DOWN)) { trace("Pushing the DOWN button! This will only log once until the button isn't pressed."); controllers.Keyboard.consume(flash.ui.Keyboard.DOWN); } }