KeyCode display sample
キーコード表示サンプル
// キーコード表示サンプル
package {
import flash.display.*;
import flash.text.*;
import flash.events.*;
import flash.ui.Keyboard;
[SWF(width="465", height="465", backgroundColor="0x000000", frameRate="30")]
public class KeySample extends Sprite {
// テキスト表示部分
private var textarea:TextField;
public function KeySample() {
var txtFmt:TextFormat = new TextFormat();
txtFmt.font = 'Courier New'; // 等幅フォント
txtFmt.color = '0xFFFFFF';
txtFmt.size = 10;
textarea = new TextField();
textarea.defaultTextFormat = txtFmt;
textarea.height = 465;
textarea.width = 465;
textarea.multiline = true;
textarea.antiAliasType = AntiAliasType.ADVANCED;
addChild(textarea);
// キーボードイベントの登録
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownListener);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpListener);
}
public function appendText(text:String):void {
textarea.replaceText(0, 0, text + "\n");
}
public function keyDownListener(e:KeyboardEvent):void {
e.preventDefault(); // しないと[PAGEDOWN]キーなどで表示が変化する
var text:String = "KeyDown: " + keyToString(e);
appendText(text);
}
public function keyUpListener(e:KeyboardEvent):void {
e.preventDefault();
var text:String = "KeyUp : " + keyToString(e);
appendText(text);
}
private function keyToString(e:KeyboardEvent):String {
var char:String;
switch (e.keyCode) {
case Keyboard.BACKSPACE:
char = '[BS]'; break;
case Keyboard.CAPS_LOCK:
char = '[CAPS]'; break;
case Keyboard.CONTROL:
char = '[CTRL]'; break;
case Keyboard.DELETE:
char = '[DELETE]'; break;
case Keyboard.DOWN:
char = '[↓]'; break;
case Keyboard.END:
char = '[END]'; break;
case Keyboard.ENTER:
char = '[ENTER]'; break;
case Keyboard.ESCAPE:
char = '[ESC]'; break;
case Keyboard.F1:
char = '[F1]'; break;
case Keyboard.F2:
char = '[F2]'; break;
case Keyboard.F3:
char = '[F3]'; break;
case Keyboard.F4:
char = '[F4]'; break;
case Keyboard.F5:
char = '[F5]'; break;
case Keyboard.F6:
char = '[F6]'; break;
case Keyboard.F7:
char = '[F7]'; break;
case Keyboard.F8:
char = '[F8]'; break;
case Keyboard.F9:
char = '[F9]'; break;
case Keyboard.F10:
char = '[F10]'; break;
case Keyboard.F11:
char = '[F11]'; break;
case Keyboard.F12:
char = '[F12]'; break;
case Keyboard.F13:
char = '[F13]'; break;
case Keyboard.F14:
char = '[F14]'; break;
case Keyboard.F15:
char = '[F15]'; break;
case Keyboard.HOME:
char = '[HOME]'; break;
case Keyboard.INSERT:
char = '[INS]'; break;
case Keyboard.LEFT:
char = '[←]'; break;
case Keyboard.PAGE_DOWN:
char = '[PAGEDOWN]'; break;
case Keyboard.PAGE_UP:
char = '[PAGEUP]'; break;
case Keyboard.RIGHT:
char = '[→]'; break;
case Keyboard.SHIFT:
char = '[SHIFT]'; break;
case Keyboard.SPACE:
char = '[SPACE]'; break;
case Keyboard.TAB:
char = '[TAB]'; break;
case Keyboard.UP:
char = '[↑]'; break;
case 18:
char = '[ALT]'; break;
case 19:
char = '[Pause]'; break;
case 44:
char = '[PrintScreen]'; break;
case 91:
case 92:
char = '[Win]'; break;
case 93:
char = '[App]'; break;
case 229:
char = '[全角/半角]'; break;
/*
case Keyboard.XXX:
char = '[XXX]'; break;
*/
default:
{
var code:uint = e.charCode;
char = (code != 0) ? String.fromCharCode(e.charCode) : "-";
}
}
return char + " (" + e.keyCode + ")";
}
}
}