flex 里键盘事件
一、
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
addedToStage="stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown)"
click="clickEvt(event)"
layout="absolute"
frameRate="6"
creationComplete="start()" keyFocusChange="testKey(event)"
backgroundGradientAlphas="[1.0, 1.0]" backgroundGradientColors="[#990000, #FF9900]">
<mx:Script source="SnakeMain.as"/>
二、<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
applicationComplete="appComplete();">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import flash.events.KeyboardEvent;
//监听keyboard事件
private function appComplete():void
{
this.addEventListener(KeyboardEvent.KEY_DOWN,keyHandler);
//this.addEventListener(KeyboardEvent.KEY_UP, myKeyUp);
//this.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);
}
private function keyHandler(event:KeyboardEvent):void
{
trace(event.charCode);
}
//响应keyup事件
private function myKeyUp(evt:KeyboardEvent):void
{
mx.controls.Alert.show('evt.keyCode=' + evt.keyCode.toString()+"\n"+ "弹起\n", "KeyUp");
}
//响应keydown事件
private function myKeyDown(evt:KeyboardEvent):void
{
t1.text=t1.text + 'evt.charCode=' + evt.charCode + ' evt.keyCode=' + evt.keyCode + ' evt.keyLocation=' + evt.keyLocation + " 按下\n";
}
]]>
</mx:Script>
<mx:TextArea x="261"
y="226"
id="t1"
width="381"
height="200"/>
<mx:TextInput x="372"
y="114"
id="t2"/>
</mx:Application>
当你把所有方法写在一个as文件里时用第一种方法获取键盘事件
当在mxml页面里获取键盘事件时用第二种方法!!!!!
不管怎么去处理,都要找准给谁添加键盘监听事件 ok 这样就哦了!!