一、   关于键盘事件<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

程序响应一个按键或一系列按键并执行一些动作,例如Control+q退出程序。Flex支持所有来自后台操作系统的所有组合键操作,它同样允许你覆盖或捕获任何按键或组合键爱执行一个用户自定义动作。

 

1.     处理键盘事件

在一些情况下,你希望捕捉全局的按键事件,不管用户在程序的哪个地方,他们的按键操作都会被程序检测到,并且动作会被执行。F

 

一个处理全局按键事件的通用方法是在application创建一个KeyboardEvent.KEY_DOWNKeyboardEvent.KEY_UP事件监听器。不管焦点在哪儿(只要焦点在程序中,而不是在浏览器控件上或浏览器之外),只要键盘被敲击,application容器上的监听器都将被触发。在处理器内,你可以使用KeyboardEvent类的charCodekeyCode属性检查键位码或者字符码,如下所示:

 

<!-- events/TrapAllKeys.mxml -->

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="initApp();">

<mx:Script><![CDATA[

private function initApp():void {

application.addEventListener(KeyboardEvent.KEY_UP, keyHandler);

}

private function keyHandler(event:KeyboardEvent):void {

t1.text = event.keyCode + "/" + event.charCode;

}

]]></mx:Script>

<mx:TextInput id="myTextInput"/>

<mx:Text id="t1"/>

</mx:Application>

 

要运行这个例子,你必须首先将焦点设定到程序中的某个东西上,例如文本输入控件。

 

因为任何集成了UIComponent的类都可以调度keyUpkeyDown事件,你也可以捕捉当焦点位于特定组件内时的键盘敲击事件。如下所示:

 

<?xml version="1.0"?>

<!-- events/TrapKeysOnTextArea.mxml -->

<mx:Application

xmlns:mx="http://www.adobe.com/2006/mxml"

creationComplete="initApp();">

<mx:Script><![CDATA[

private function initApp():void {

myTextInput.addEventListener(KeyboardEvent.KEY_UP, keyHandler);

}

private function keyHandler(event:KeyboardEvent):void {

t1.text = event.keyCode + "/" + event.charCode;

}

]]></mx:Script>

<mx:TextInput id="myTextInput"/>

<mx:Text id="t1"/>

</mx:Application>

 

2.     了解键位码和字符码属性

你可以通过访问keyCodeharCode属性来确定到底是哪个键被敲击,并触发另一个动作作为结果。keyCode属性是一个数字值,它相当键盘上的建的值;charCode属性是建在当前字符集中的数字值(默认的字符集是UTF-8)。两者主要的区别在于,keyCode描述了键盘上的一个特殊的建(第一行中的1和小键盘中的1是不同的键,但第一行的1和!是相同的键)charCode描述了一个特殊的字符(Rr是不同的)

 

         键和键位值的映射关系依赖于设备(键盘)和操作系统。

 

         下面的例子展示了你敲击的键的字符和键位码:

 

<?xml version="1.0"?>

<!-- charts/ShowCharAndKeyCodes.mxml -->

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()">

<mx:Script><![CDATA[

import flash.events.KeyboardEvent;

private function init():void {

ti1.setFocus();

this.addEventListener(KeyboardEvent.KEY_DOWN, trapKeys);

}

private function trapKeys(e:KeyboardEvent):void {

ta1.text = String(e.toString());

l1.text = numToChar(e.charCode) + " (" + String(e.charCode) + ")";

l2.text = numToChar(e.keyCode) + " (" + String(e.keyCode) + ")";

}

private function numToChar(num:int):String {

if (num > 47 && num < 58) {

var strNums:String = "0123456789";

return strNums.charAt(num - 48);

} else if (num > 64 && num < 91) {

var strCaps:String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

return strCaps.charAt(num - 65);

} else if (num > 96 && num < 123) {

var strLow:String = "abcdefghijklmnopqrstuvwxyz";

return strLow.charAt(num - 97);

} else {

return num.toString();

}

}

]]></mx:Script>

<mx:TextInput width="50%" id="ti1"/>

<mx:Canvas id="mainCanvas" width="100%" height="100%">

<mx:Form>

<mx:FormItem label="Char (Code)">

<mx:Label id="l1"/>

</mx:FormItem>

<mx:FormItem label="Key (Code)">

<mx:Label id="l2"/>

</mx:FormItem>

<mx:FormItem label="Key Event">

<mx:TextArea id="ta1" width="500" height="200" editable="false"/>

</mx:FormItem>

</mx:Form>

</mx:Canvas>

</mx:Application>

 

你可以通过使用条件运算符来监听特定的键或组合键:

 

<?xml version="1.0"?>

<!-- events/TrapQKey.mxml -->

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="initApp();">

<mx:Script><![CDATA[

private function initApp():void {

application.addEventListener(KeyboardEvent.KEY_UP,keyHandler);

// Set the focus somewhere inside the application.

ta1.setFocus();

}

//This function quits the application if the user presses Shift+Q.

private function keyHandler(event:KeyboardEvent):void {

var bShiftPressed:Boolean = event.shiftKey;

if (bShiftPressed) {

var curKeyCode:int = event.keyCode;

if (curKeyCode == 81) { // 81 is the keycode value for the Q key

/* Quit the application by closing the browser using JavaScript.

This may not work in all browsers. */

var url:URLRequest = new

URLRequest("javascript:window.close()");

navigateToURL(url,"_self");

}

}

}

]]></mx:Script>

<mx:TextArea id="ta1" text="Focus here so that Shift+Q will quit the browser."/>

</mx:Application>