<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Flex the world &#187; 发送信息</title>
	<atom:link href="http://www.flextheworld.com/tag/%e5%8f%91%e9%80%81%e4%bf%a1%e6%81%af/feed" rel="self" type="application/rss+xml" />
	<link>http://www.flextheworld.com</link>
	<description>Flex, AIR, FMS, P2P and Things......</description>
	<lastBuildDate>Fri, 10 Sep 2010 08:08:54 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>完美实现TextArea内按Enter发送消息并清空内容</title>
		<link>http://www.flextheworld.com/2009/08/textarea-enter-send-message.html</link>
		<comments>http://www.flextheworld.com/2009/08/textarea-enter-send-message.html#comments</comments>
		<pubDate>Thu, 06 Aug 2009 12:44:34 +0000</pubDate>
		<dc:creator>Albert Zhang</dc:creator>
				<category><![CDATA[Flex 迷你教程]]></category>
		<category><![CDATA[TestArea]]></category>
		<category><![CDATA[发送信息]]></category>

		<guid isPermaLink="false">http://www.flextheworld.com/?p=564</guid>
		<description><![CDATA[利用KeyboardEvent.KEY_DOWN和TextEvent.TEXT_INPUT事件结合，可以用FlashPlayer 10中的TextArea完美实现消息发送框的功能。包括：
按Enter键立即发送消息、消息发送后立即清空TextArea、同时按Ctrl/Shift+Enter回车换行。
原理是：先判断KEY_DOWN事件中按下的是什么键，如果是Shift/Ctrl+Enter，则用代码向TextArea中插入一个换行符（这里flashplayer 9和10不一样，在9中按ctrl+Enter默认就是换行，而10不会，所以要用代码插入）。如果只按下了Enter，则在TEXT_INPUT事件处理中用preventDefault()来阻止默认的换行动作。
代码如下：

?View Code ACTIONSCRIPT3//下面三个变量用来保存KEY_DOWN事件时键盘的状态
&#160;
var shiftKey:Boolean;
&#160;
var ctrlKey:Boolean;
&#160;
var keyCode:int;
&#160;
var messageInput:TextArea;
&#160;
messageInput.addEventListener&#40;KeyboardEvent.KEY_DOWN,onKeyDown&#41;;
&#160;
messageInput.addEventListener&#40;TextEvent.TEXT_INPUT,onTextInput&#41;;
&#160;
private function onKeyDown&#40;e:KeyboardEvent&#41;:void&#123;
&#160;
//如果按下Enter时还按下Shift/Ctrl，则换行
&#160;
if&#40; e.keyCode==Keyboard.ENTER &#38;amp;&#38;amp; &#40;e.shiftKey &#124;&#124; e.ctrlKey&#41; &#41;&#123;
&#160;
callLater&#40;function&#40;&#41;:void&#123; //注意这个callLater
&#160;
var msg:String = messageInput.text;
&#160;
var part1:String = msg.substring&#40;0, messageInput.selectionBeginIndex&#41;;
&#160;
var part2:String = msg.substring&#40;messageInput.selectionEndIndex, 0x7fffffff&#41;;
&#160;
var selIndex:int = messageInput.selectionBeginIndex + 1;
&#160;
messageInput.text = part1 + &#34;\n&#34; + part2;
&#160;
messageInput.setSelection&#40;selIndex,selIndex&#41;;
&#160;
&#125;&#41;;
&#160;
keyCode = -1; //标识本次按键不需要TEXT_INPUT事件配合处理
&#160;
return;
&#160;
&#125;
&#160;
shiftKey = e.shiftKey;
&#160;
ctrlKey = e.ctrlKey;
&#160;
keyCode = e.keyCode;
&#160;
&#125;
&#160;
private function onTextInput&#40;evt:TextEvent&#41;:void&#123;
&#160;
if&#40;keyCode == -1&#41;&#123;
&#160;
return;
&#160;
&#125;
&#160;
if&#40;keyCode==Keyboard.ENTER &#38;amp;&#38;amp; !shiftKey [...]]]></description>
			<content:encoded><![CDATA[<p>利用KeyboardEvent.KEY_DOWN和TextEvent.TEXT_INPUT事件结合，可以用FlashPlayer 10中的TextArea完美实现消息发送框的功能。包括：</p>
<p>按Enter键立即发送消息、消息发送后立即清空TextArea、同时按Ctrl/Shift+Enter回车换行。</p>
<p>原理是：先判断KEY_DOWN事件中按下的是什么键，如果是Shift/Ctrl+Enter，则用代码向TextArea中插入一个换行符（这里flashplayer 9和10不一样，在9中按ctrl+Enter默认就是换行，而10不会，所以要用代码插入）。如果只按下了Enter，则在TEXT_INPUT事件处理中用preventDefault()来阻止默认的换行动作。</p>
<p>代码如下：<span id="more-564"></span></p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p564code2'); return false;">View Code</a> ACTIONSCRIPT3</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p5642"><td class="code" id="p564code2"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900; font-style: italic;">//下面三个变量用来保存KEY_DOWN事件时键盘的状态</span>
&nbsp;
<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">shiftKey</span><span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=boolean%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:boolean.html"><span style="color: #004993;">Boolean</span></a><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">ctrlKey</span><span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=boolean%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:boolean.html"><span style="color: #004993;">Boolean</span></a><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">keyCode</span><span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #6699cc; font-weight: bold;">var</span> messageInput<span style="color: #000066; font-weight: bold;">:</span>TextArea<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
messageInput<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=keyboardevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:keyboardevent.html"><span style="color: #004993;">KeyboardEvent</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">KEY_DOWN</span><span style="color: #000066; font-weight: bold;">,</span>onKeyDown<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
messageInput<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=textevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:textevent.html"><span style="color: #004993;">TextEvent</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">TEXT_INPUT</span><span style="color: #000066; font-weight: bold;">,</span>onTextInput<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> onKeyDown<span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=keyboardevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:keyboardevent.html"><span style="color: #004993;">KeyboardEvent</span></a><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span>
&nbsp;
<span style="color: #009900; font-style: italic;">//如果按下Enter时还按下Shift/Ctrl，则换行</span>
&nbsp;
<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span> e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">keyCode</span>==<a href="http://www.google.com/search?q=keyboard%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:keyboard.html"><span style="color: #004993;">Keyboard</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ENTER</span> <span style="color: #000066; font-weight: bold;">&amp;</span>amp<span style="color: #000066; font-weight: bold;">;&amp;</span>amp<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">shiftKey</span> <span style="color: #000066; font-weight: bold;">||</span> e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ctrlKey</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
callLater<span style="color: #000000;">&#40;</span><span style="color: #339966; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span> <span style="color: #009900; font-style: italic;">//注意这个callLater</span>
&nbsp;
<span style="color: #6699cc; font-weight: bold;">var</span> msg<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=string%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:string.html"><span style="color: #004993;">String</span></a> = messageInput<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #6699cc; font-weight: bold;">var</span> part1<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=string%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:string.html"><span style="color: #004993;">String</span></a> = msg<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">substring</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> messageInput<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">selectionBeginIndex</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #6699cc; font-weight: bold;">var</span> part2<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=string%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:string.html"><span style="color: #004993;">String</span></a> = msg<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">substring</span><span style="color: #000000;">&#40;</span>messageInput<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">selectionEndIndex</span><span style="color: #000066; font-weight: bold;">,</span> 0x7fffffff<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #6699cc; font-weight: bold;">var</span> selIndex<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a> = messageInput<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">selectionBeginIndex</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
messageInput<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span> = part1 <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #990000;">&quot;<span style="">\n</span>&quot;</span> <span style="color: #000066; font-weight: bold;">+</span> part2<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
messageInput<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">setSelection</span><span style="color: #000000;">&#40;</span>selIndex<span style="color: #000066; font-weight: bold;">,</span>selIndex<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #004993;">keyCode</span> = <span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">//标识本次按键不需要TEXT_INPUT事件配合处理</span>
&nbsp;
<span style="color: #0033ff; font-weight: bold;">return</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #004993;">shiftKey</span> = e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">shiftKey</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #004993;">ctrlKey</span> = e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ctrlKey</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #004993;">keyCode</span> = e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">keyCode</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> onTextInput<span style="color: #000000;">&#40;</span>evt<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=textevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:textevent.html"><span style="color: #004993;">TextEvent</span></a><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span>
&nbsp;
<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">keyCode</span> == <span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
<span style="color: #0033ff; font-weight: bold;">return</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">keyCode</span>==<a href="http://www.google.com/search?q=keyboard%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:keyboard.html"><span style="color: #004993;">Keyboard</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ENTER</span> <span style="color: #000066; font-weight: bold;">&amp;</span>amp<span style="color: #000066; font-weight: bold;">;&amp;</span>amp<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000066; font-weight: bold;">!</span><span style="color: #004993;">shiftKey</span> <span style="color: #000066; font-weight: bold;">&amp;</span>amp<span style="color: #000066; font-weight: bold;">;&amp;</span>amp<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000066; font-weight: bold;">!</span><span style="color: #004993;">ctrlKey</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
evt<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">preventDefault</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
callLater<span style="color: #000000;">&#40;</span><span style="color: #339966; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span>
&nbsp;
<span style="color: #009900; font-style: italic;">//sendMessage()即为发送消息的方法，它应该返回一个Boolean值，以此来决定是否清空消息框</span>
&nbsp;
<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span> sendMessage<span style="color: #000000;">&#40;</span>messageInput<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
messageInput<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span> = <span style="color: #990000;">&quot;&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
messageInput<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">htmlText</span> = <span style="color: #990000;">&quot;&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
messageInput<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">setSelection</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.flextheworld.com/2009/08/textarea-enter-send-message.html/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
