//キーイベントの処理
class KeyEx {
private var label:TextField;//ラベル
//コンストラクタ
function KeyEx (mc:MovieClip) {
//ラベルの追加
label=addLabel(mc,"label","キーイベント");
//キーリスナーの指定
Key.addListener(this);
}
//キーダウンイベントの処理
private function onKeyDown() {
update(true);
}
//キーアップイベントの処理
private function onKeyUp() {
update(false);
}
//更新
private function update(down:Boolean):Void {
var text:String=(down)?"キーダウン\n":"キーアップ\n";
text+=
"キーコード:"+Key.getCode()+"\n"+
"コントロールキー:"+Key.isDown(Key.CONTROL)+"\n"+
"シフトキー:"+Key.isDown(Key.SHIFT)+"\n";
label.text=text;
}
//ラベルの追加
private function addLabel(mc:MovieClip,name:String,
text:String,x:Number,y:Number,format:TextFormat):TextField {
//初期値
if (text==undefined) text="";
if (x ==undefined) x=0;
if (y ==undefined) y=0;
//テキストフィールドの追加
mc.createTextField(name,mc.getNextHighestDepth(),0,0,0,0);
mc[name].autoSize ="left";//オートサイズ
mc[name].selectable=false; //選択不可
mc[name].text=text; //テキスト
mc[name]._x =x; //X座標
mc[name]._y =y; //Y座標
if (format!=undefined) mc[name].setTextFormat(format);//書式
return mc[name];
}
//メイン
static function main() {
var app:KeyEx=new KeyEx(_root);
}
}
|