经过测试,ctrl键和enter键组合的话,在flash player里是没有效果的,按下了 ctrl时,再按enter键,就监听不到enter键的按下。但是用IE打开可以(测试时用的IE8).
写了个简单的按键管理类:
private var isctrlDown:Boolean = false; //-------------------------------------------------------------------------- // Public Methods //-------------------------------------------------------------------------- //-------------------------------------------------------------------------- // Event Handler //-------------------------------------------------------------------------- private function onKeyUpHandler(event:KeyboardEvent):void { if(event.keyCode == Keyboard.CONTROL) { isctrlDown = false; // trace("控制键弹起"); } } private function onKeyDownHandler(event:KeyboardEvent):void { if(event.keyCode == Keyboard.CONTROL) { isctrlDown = true; // trace("控制键按下"); return; } // trace(event.keyCode) if(event.keyCode == 13) { // trace("ENTER按下"); if(isctrlDown == true) { // trace("ENTER按下并且ctrl按下"); isctrlDown = false; dispatchEvent(new PrivateChatEvent(PrivateChatEvent.CTRL_ENTER_SEND_MSG)); } } } //-------------------------------------------------------------------------- // Protected //-------------------------------------------------------------------------- //-------------------------------------------------------------------------- // Privates //-------------------------------------------------------------------------- private function init():void { listenKeyBoardEvent(); } private function listenKeyBoardEvent():void { Application.application.stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDownHandler); Application.application.stage.addEventListener(KeyboardEvent.KEY_UP,onKeyUpHandler); } //-------------------------------------------------------------------------- // Dispose //-------------------------------------------------------------------------- public function destroy():void { Application.application.stage.removeEventListener(KeyboardEvent.KEY_DOWN,onKeyDownHandler); Application.application.stage.removeEventListener(KeyboardEvent.KEY_UP,onKeyUpHandler); }
FLex项目中若用TextArea做为聊天内容输入框,在用ctrl+enter键发送了聊天信息后的那一刹那,光标可能会向下换一行,并且输入框可能会随之出现文本残留,一个解决办法就是做个延时,TextArea.editable=false,让TextArea在延时到了才能再被输入文本,TextArea.editable = true ,延时长度为 50毫秒感觉还不错,在延时到了时先
让TextArea.text = "",可以解决文本发送出去后的残留问题。