In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

TinierKeyboard

Get Adobe Flash player
by grapefrukt 24 Feb 2009
    Embed
package {
    import flash.display.Sprite;
    public class STKI extends Sprite {
 
        // this stores all key states
        // For some reason this seems to be smaller when typed
        public var k:Object;
 
        private var block:Sprite;
 
        public function STKI () {
            k = { };    // shorthand for initializing a object
			
	    // the trick is to use the dynamic nature of objects, 
	    // if the property exists it's overwritten, 
	    // if it doesn't it's created
	    // the function actually gets a KeyboardEvent, but
	    // having it untyped makes it smaller
	    // So does using a regular ("keyDown") string instead 
	    // of the static one provided by the event. 
            stage.addEventListener("keyDown", function(e:*):void{ k[e["keyCode"]] = true});
            stage.addEventListener("keyUp", function(e:*):void{ k[e["keyCode"]] = false});
 
            // this is just to show that it works
            block = new Sprite();
            block.graphics.beginFill(0xff00ff);
            block.graphics.drawRect(-4, -4, 8, 8);
            block.x = 250;
            block.y = 250;
            addChild(block);
            addEventListener("enterFrame", handleEnterFrame);
        }
 
 
        private function handleEnterFrame(e:*):void{
 
            // this is how you use it, just access the keyCode
            // in the object, it acts just as good old Key.isDown
            // from AS2
 
            if (k[37]) { //left
                block.x -= 1;
            } else if (k[39]) { // right
                block.x += 1;
            }
 
            if (k[38]){ // up
                block.y -= 1;
            } else if (k[40]) { // down
                block.y += 1;
            }
        }
 
    }
}