//下面三个变量用来保存KEY_DOWN事件时键盘的状态 var shiftKey:Boolean; var ctrlKey:Boolean; var keyCode:int; var messageInput:TextArea; messageInput.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown); messageInput.addEventListener(TextEvent.TEXT_INPUT,onTextInput); private function onKeyDown(e:KeyboardEvent):void{ //如果按下Enter时还按下Shift/Ctrl,则换行 if( e.keyCode==Keyboard.ENTER && (e.shiftKey || e.ctrlKey) ){ callLater(function():void{ //注意这个callLater var msg:String = messageInput.text; var part1:String = msg.substring(0, messageInput.selectionBeginIndex); var part2:String = msg.substring(messageInput.selectionEndIndex, 0x7fffffff); var selIndex:int = messageInput.selectionBeginIndex + 1; messageInput.text = part1 + "\n" + part2; messageInput.setSelection(selIndex,selIndex); }); keyCode = -1; //标识本次按键不需要TEXT_INPUT事件配合处理 return; } shiftKey = e.shiftKey; ctrlKey = e.ctrlKey; keyCode = e.keyCode; } private function onTextInput(evt:TextEvent):void{ if(keyCode == -1){ return; } if(keyCode==Keyboard.ENTER && !shiftKey && !ctrlKey){ evt.preventDefault(); callLater(function():void{ //sendMessage()即为发送消息的方法,它应该返回一个Boolean值,以此来决定是否清空消息框 if( sendMessage(messageInput.text) ){ messageInput.text = ""; messageInput.htmlText = ""; messageInput.setSelection(0,0); } }); } }
TextArea内按Enter发送消息并清空内容 换行
最新推荐文章于 2021-11-16 22:02:56 发布