Skip to content

Instantly share code, notes, and snippets.

@girasquid
Created September 29, 2015 00:26
Show Gist options
  • Save girasquid/dcf7b0f05fcd3930d37f to your computer and use it in GitHub Desktop.
Save girasquid/dcf7b0f05fcd3930d37f to your computer and use it in GitHub Desktop.

Revisions

  1. girasquid revised this gist Sep 29, 2015. No changes.
  2. girasquid created this gist Sep 29, 2015.
    94 changes: 94 additions & 0 deletions Keyboard.as
    Original 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;
    }
    }
    }
    15 changes: 15 additions & 0 deletions Usage.as
    Original 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);
    }
    }