private var desction:Array = [0,0,0,0];
private function onKeyDownHandler(_evt:KeyboardEvent):void {
switch (_evt.keyCode) {
case 37:
desction[0] = 1;
break;
case 38:
desction[1] = 1;
break;
case 39:
desction[2] = 1;
break;
case 40:
desction[3] = 1;
break;
default:
break;
}
//txt.text = desction.toString();
trace("值:" + desction.toString())
downTest(desction.join(""));
}
private function downTest(k:String):void {
switch (k) {
case "0110":
txt.text = " 上+右";
break;
case "1100":
txt.text = " 上+左";
break;
case "0011":
txt.text = " 下+右";
break;
case "1001":
txt.text = " 下+左";
break;
case "1010":
txt.text = " 左+右";
break;
case "0101":
txt.text = " 上+下";
break;
case "1000":
txt.text = " 左";
break;
case "0100":
txt.text = " 上";
break;
case "0010":
txt.text = " 右";
break;
case "0001":
txt.text = " 下";
break;
default:
break;
}
}
private function onKeyUpHandler(_evt:KeyboardEvent):void {
switch (_evt.keyCode) {
case 37:
desction[0] = 0;
break;
case 38:
desction[1] = 0;
break;
case 39:
desction[2] = 0;
break;
case 40:
desction[3] = 0;
break;
default:
break;
}
}
private function onKeyDownHandler(_evt:KeyboardEvent):void {
switch (_evt.keyCode) {
case 37:
desction[0] = 1;
break;
case 38:
desction[1] = 1;
break;
case 39:
desction[2] = 1;
break;
case 40:
desction[3] = 1;
break;
default:
break;
}
//txt.text = desction.toString();
trace("值:" + desction.toString())
downTest(desction.join(""));
}
private function downTest(k:String):void {
switch (k) {
case "0110":
txt.text = " 上+右";
break;
case "1100":
txt.text = " 上+左";
break;
case "0011":
txt.text = " 下+右";
break;
case "1001":
txt.text = " 下+左";
break;
case "1010":
txt.text = " 左+右";
break;
case "0101":
txt.text = " 上+下";
break;
case "1000":
txt.text = " 左";
break;
case "0100":
txt.text = " 上";
break;
case "0010":
txt.text = " 右";
break;
case "0001":
txt.text = " 下";
break;
default:
break;
}
}
private function onKeyUpHandler(_evt:KeyboardEvent):void {
switch (_evt.keyCode) {
case 37:
desction[0] = 0;
break;
case 38:
desction[1] = 0;
break;
case 39:
desction[2] = 0;
break;
case 40:
desction[3] = 0;
break;
default:
break;
}
}
对象应用:
var key_obj:Object = new Object();
private function onKeyDownHandler1(_evt:KeyboardEvent):void {
var _code:String = String.fromCharCode(_evt.charCode);
key_obj[_code] = true;
for (var p:String in key_obj){ //用于其它字母和数字鍵
trace("当前按下的键有:" + p + "值=true(key_obj[p])");
}
/*_evt.ctrlKey && trace("Ctrl键被按下");
_evt.altKey && trace("Alt键被按下");
_evt.shiftKey && trace("Shift键被按下");*/
}
private function onKeyUpHandler1(_evt:KeyboardEvent):void {
var _code:String = String.fromCharCode(_evt.charCode);
key_obj[_code] = null;
delete key_obj[_code];
}
var _code:String = String.fromCharCode(_evt.charCode);
key_obj[_code] = true;
for (var p:String in key_obj){ //用于其它字母和数字鍵
trace("当前按下的键有:" + p + "值=true(key_obj[p])");
}
/*_evt.ctrlKey && trace("Ctrl键被按下");
_evt.altKey && trace("Alt键被按下");
_evt.shiftKey && trace("Shift键被按下");*/
}
private function onKeyUpHandler1(_evt:KeyboardEvent):void {
var _code:String = String.fromCharCode(_evt.charCode);
key_obj[_code] = null;
delete key_obj[_code];
}