Created
September 29, 2015 00:26
-
-
Save girasquid/dcf7b0f05fcd3930d37f to your computer and use it in GitHub Desktop.
Kinda-bad keyboard button tracking class with a similar interface to Controllers.as: https://gist.github.com/girasquid/50c7c9f2de45a6079e4c
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 characters
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 characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment