キーボードの状態をチェックする

by toburau
キー入力の判定
♥0 | Line 144 | Modified 2010-08-26 09:41:11 | MIT License
play

ActionScript3 source code

/**
 * Copyright toburau ( http://wonderfl.net/user/toburau )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/d3H5
 */

// キー入力の判定
package {
    import flash.display.Sprite;
    import flash.events.KeyboardEvent;
    import flash.events.Event;
    import flash.ui.Keyboard;
    public class Keytest extends Sprite {
        private var player:Sprite;
        public function Keytest() {
            Key.init(stage);
            
            player = new Sprite();
            player.graphics.beginFill(0xff0000);
            player.graphics.drawCircle(0,0,50);
            player.graphics.endFill();
            player.x = stage.stageWidth/2;
            player.y = stage.stageHeight/2;
            addChild(player);
            
            stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }
        
        public function onEnterFrame(event:Event):void {
            if(Key.isPress(Key.UP)) {
                player.y -= 10;
            }
            if(Key.isPress(Key.DOWN)) {
                player.y += 10;
            }
            if(Key.isPress(Key.LEFT)) {
                player.x -= 10;
            }
            if(Key.isPress(Key.RIGHT)) {
                player.x += 10;
            }
            if(Key.isPush(Key.SPACE)) {
                if(contains(player)) {
                    removeChild(player);
                } else {
                    addChild(player);
                }
            }

            Key.update();
        }
    }
}

// キーボードからの入力をチェックするクラスKey
// 使用法:
// 起動時にinit()で初期化する。引数はstage。
//     Key.init(stage);
// フレーム更新処理中キー入力の判定が終わったあとにupdate()を呼び出す。
//     Key.update();
// isPress(keycode)で押されているか取得。
// isPush(keycode)で押されていない状態から押されたときを取得
// 引数に渡すkeycodeは次のとおり。
//    UP カーソル上
//  DOWN カーソル下
//  LEFT カーソル左
// RIGHT カーソル右
// SPACE スペースキー
// ENTER Enterキー
//
// 使用例
//   onFrameUpdate() {
//        if( Key.isPress(Key.UP) ) {
//            // カーソル上が押されているときの処理
//        }
//        ...
//        Key.update(); // キーの判定が終わったらupdate()を呼ぶ
//    }
//
import flash.display.InteractiveObject;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.ui.Keyboard;

class Key
{
    public static const    UP:uint = 0;
    public static const  DOWN:uint = 1;
    public static const  LEFT:uint = 2;
    public static const RIGHT:uint = 3;
    public static const SPACE:uint = 4;
    public static const ENTER:uint = 5;
    private static const CODEMAX:uint = 6;

    private static var m_Press:Vector.<Boolean> = new Vector.<Boolean>(CODEMAX);
    private static var m_Push:Vector.<Boolean> = new Vector.<Boolean>(CODEMAX);
    
    public static function init(target:InteractiveObject):void {
        target.stage.focus = target;
        for(var i:int=0; i<CODEMAX; i++) {
            m_Press[i] = false;
            m_Push[i] = false;
        }
        
        target.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
        target.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
    }
    public static function update():void {
        for(var i:int=0; i<CODEMAX; i++) {
            m_Push[i] = false;
        }
    }
    
    public static function isPress(keycode:uint):Boolean {
        return m_Press[keycode];
    }
    public static function isPush(keycode:uint):Boolean {
        return m_Push[keycode];
    }

    private static function onKeyDown(event:KeyboardEvent):void {
        switch(event.keyCode) {
            case Keyboard.UP:
            if(m_Press[UP] == false) {
                m_Push[UP] = true;
            }
            m_Press[UP] = true;
            break;
            case Keyboard.DOWN:
            if(m_Press[DOWN] == false) {
                m_Push[DOWN] = true;
            }
            m_Press[DOWN] = true;
            break;
            case Keyboard.LEFT:
            if(m_Press[LEFT] == false) {
                m_Push[LEFT] = true;
            }
            m_Press[LEFT] = true;
            break;
            case Keyboard.RIGHT:
            if(m_Press[RIGHT] == false) {
                m_Push[RIGHT] = true;
            }
            m_Press[RIGHT] = true;
            break;
            case Keyboard.SPACE:
            if(m_Press[SPACE] == false) {
                m_Push[SPACE] = true;
            }
            m_Press[SPACE] = true;
            break;
            case Keyboard.ENTER:
            if(m_Press[ENTER] == false) {
                m_Push[ENTER] = true;
            }
            m_Press[ENTER] = true;
            break;
            default:
            break;
        }
    }
    private static function onKeyUp(event:KeyboardEvent):void {
        switch(event.keyCode) {
            case Keyboard.UP:
            m_Press[UP] = false;
            break;
            case Keyboard.DOWN:
            m_Press[DOWN] = false;
            break;
            case Keyboard.LEFT:
            m_Press[LEFT] = false;
            break;
            case Keyboard.RIGHT:
            m_Press[RIGHT] = false;
            break;
            case Keyboard.SPACE:
            m_Press[SPACE] = false;
            break;
            case Keyboard.ENTER:
            m_Press[ENTER] = false;
            break;
            default:
            break;
        }
    }
}