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

キーボードイベントの練習

画面をクリックしてキーボードの入力を半角英数状態にして
テンキーで数値を入力、アルファベットを入力すると
画面に押したキーを表示します。
Get Adobe Flash player
by yuugurenote 28 Nov 2011
    Embed
/**
 * Copyright yuugurenote ( http://wonderfl.net/user/yuugurenote )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/vI9J
 */

package {
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.events.KeyboardEvent;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFormat;
    import flash.text.TextColorType;
    import flash.geom.ColorTransform;
    [SWF(width=465,height=465,frameRate=60)]

    public class AS111128_01 extends Sprite {
        public var sw:Number=stage.stageWidth;
        public var sh:Number=stage.stageHeight;

        public function AS111128_01() {
            this.graphics.beginFill(0x000000,1);
            this.graphics.drawRect(0,0,sw,sh);
            this.graphics.endFill();
            stage.addEventListener(KeyboardEvent.KEY_DOWN,xKeyDown);
        }
        public function xKeyDown(e:KeyboardEvent):void {
            var _mySprite:Sprite = new Sprite();
            _mySprite.x=sw/2;
            _mySprite.y=sh/2;
            addChild(_mySprite);

            var tf:TextField=new TextField  ;
            tf.autoSize=TextFieldAutoSize.LEFT;
            var format:TextFormat=new TextFormat(null,16);
            format.color = Math.random() * 0xFFFFFF;
            tf.defaultTextFormat=format;

            const KEY_0:uint=96;
            const KEY_1:uint=97;
            const KEY_2:uint=98;
            const KEY_3:uint=99;
            const KEY_4:uint=100;
            const KEY_5:uint=101;
            const KEY_6:uint=102;
            const KEY_7:uint=103;
            const KEY_8:uint=104;
            const KEY_9:uint=105;
            
            const KEY_A:uint=65;
            const KEY_B:uint=66;
            const KEY_C:uint=67;
            const KEY_D:uint=68;
            const KEY_E:uint=69;
            const KEY_F:uint=70;
            const KEY_G:uint=71;
            const KEY_H:uint=72;
            const KEY_I:uint=73;
            const KEY_J:uint=74;
            const KEY_K:uint=75;
            const KEY_L:uint=76;
            const KEY_M:uint=77;
            const KEY_N:uint=78;
            const KEY_O:uint=79;
            const KEY_P:uint=80;
            const KEY_Q:uint=81;
            const KEY_R:uint=82;
            const KEY_S:uint=83;
            const KEY_T:uint=84;
            const KEY_U:uint=85;
            const KEY_V:uint=86;
            const KEY_W:uint=87;
            const KEY_X:uint=88;
            const KEY_Y:uint=89;
            const KEY_Z:uint=90;

            switch (e.keyCode) {
                case KEY_0 :
                    tf.text=String(0);
                    break;
                case KEY_1 :
                    tf.text=String(1);
                    break;
                case KEY_2 :
                    tf.text=String(2);
                    break;
                case KEY_3 :
                    tf.text=String(3);
                    break;
                case KEY_4 :
                    tf.text=String(4);
                    break;
                case KEY_5 :
                    tf.text=String(5);
                    break;
                case KEY_6 :
                    tf.text=String(6);
                    break;
                case KEY_7 :
                    tf.text=String(7);
                    break;
                case KEY_8 :
                    tf.text=String(8);
                    break;
                case KEY_9 :
                    tf.text=String(9);
                    break;
                case KEY_A:    
                    tf.text="A"
                    break;
                case KEY_B:    
                    tf.text="B"
                    break;
                case KEY_C:    
                    tf.text="C"
                    break;
                case KEY_D:    
                    tf.text="D"
                    break;
                case KEY_E:    
                    tf.text="E"
                    break;
                case KEY_F:    
                    tf.text="F"
                    break;
                case KEY_G:    
                    tf.text="G"
                    break;
                case KEY_H:    
                    tf.text="H"
                    break;
                case KEY_I:    
                    tf.text="I"
                    break;
                case KEY_J:    
                    tf.text="J"
                    break;
                case KEY_K:    
                    tf.text="K"
                    break;
                case KEY_L:    
                    tf.text="L"
                    break;
                case KEY_M:    
                    tf.text="M"
                    break;
                case KEY_N:    
                    tf.text="N"
                    break;
                case KEY_O:    
                    tf.text="O"
                    break;
                case KEY_P:    
                    tf.text="P"
                    break;
                case KEY_Q:    
                    tf.text="Q"
                    break;
                case KEY_R:    
                    tf.text="R"
                    break;
                case KEY_S:    
                    tf.text="S"
                    break;
                case KEY_T:    
                    tf.text="T"
                    break;
                case KEY_U:    
                    tf.text="U"
                    break;
                case KEY_V:    
                    tf.text="V"
                    break;
                case KEY_W:    
                    tf.text="W"
                    break;
                case KEY_X:    
                    tf.text="X"
                    break;
                case KEY_Y:    
                    tf.text="Y"
                    break;
                case KEY_Z:    
                    tf.text="Z"
                    break;
                    
            }
            _mySprite.addEventListener(Event.ENTER_FRAME,xEnter);
            _mySprite.addChild(tf);

            var px:Number=Math.random()*sw;
            var py:Number=Math.random()*sh;

            function xEnter(e:Event):void {
                _mySprite.x += (px - _mySprite.x)/100;
                _mySprite.y += (py - _mySprite.y)/100;
                _mySprite.alpha-=0.001;
            
                if (_mySprite.alpha<0) {
                    _mySprite.removeEventListener(Event.ENTER_FRAME,xEnter);
                    removeChild(_mySprite);
                }
            }

        }
    }
}