<?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>Thanks, Mister! &#187; Event.DEACTIVATE</title>
	<atom:link href="http://www.thanksmister.com/index.php/archive/tag/event-deactivate/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thanksmister.com</link>
	<description>Adobe Flex &#38; AIR Development</description>
	<lastBuildDate>Mon, 19 Jul 2010 17:28:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Preventing scrolling and mouse events on out of focus AIR application</title>
		<link>http://www.thanksmister.com/index.php/archive/preventing-scrolling-mouse-events-out-of-focus-air-application/</link>
		<comments>http://www.thanksmister.com/index.php/archive/preventing-scrolling-mouse-events-out-of-focus-air-application/#comments</comments>
		<pubDate>Tue, 29 Sep 2009 23:28:44 +0000</pubDate>
		<dc:creator>Mister</dc:creator>
				<category><![CDATA[AIR]]></category>
		<category><![CDATA[Adobe AIR]]></category>
		<category><![CDATA[Event.ACTIVATE]]></category>
		<category><![CDATA[Event.DEACTIVATE]]></category>
		<category><![CDATA[mouseChildren]]></category>
		<category><![CDATA[mouseEnabled]]></category>

		<guid isPermaLink="false">http://thanksmister.com/?p=746</guid>
		<description><![CDATA[This has been a little bit of annoyance for me for some time. When an AIR application (like any Twitter application) is out of focus and you click on the application activate it, you inadvertently click a link or button within the application. You are also able to scroll the application when the application has [...]]]></description>
			<content:encoded><![CDATA[<p>This has been a little bit of annoyance for me for some time.  When an AIR application (like any Twitter application) is out of focus and you click on the application activate it, you inadvertently click a link or button within the application.   You are also able to scroll the application when the application has no focus, which may or may not be your desired behavior.  I wanted a way to prevent mouse events and scrolling until the application has focus.   I tried to find away around this annoyance by capturing if the application has focus by listening for Event.ACTIVATE or Event.DEACTIVATE events on the NativeApplication.nativeApplication:</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">NativeApplication.<span style="color: #006600;">nativeApplication</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ACTIVATE</span>, handleAppActivate<span style="color: #66cc66;">&#41;</span>;<br />
NativeApplication.<span style="color: #006600;">nativeApplication</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">DEACTIVATE</span>, handleAppDiactivate<span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> handleAppActivate<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// set a flag that application has focus so now links and buttons work</span><br />
<span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> handleAppDiactivate<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #808080; font-style: italic;">// set some flag to let the application has no focus so disable links and buttons</span><br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></tbody></table></div>
<p>This solution has a couple of problems.  First the application has focus the instance you click on any link within the application window.  So fine, the way around that is to use some kind of timer so that your flag that says application is now active is not set until some time after the application receives focus.     This leads to the second problem, at least for larger scale applications.   You have to pipe the flag that says the application has no focus to every button and link through the entire application, this is a huge pain.   </p>
<p><span id="more-746"></span></p>
<p>Today I just happened to be playing with the new <a href="http://www.gskinner.com/blog/archives/2009/05/idle_cpu_usage.html">FramerateThrottler</a> class created by Grant Skinner to reduce CPU usage for AIR applications running on Mac (this is yet another gripe of mine).    So on his bog there are some comments about using mouseChildren and mouseEnabled on all open AIR windows to reduce CPU.    Anyways, when I was messing around with this suggestion, I happened to notice my application no longer scrolled when the application was in the background, but mouse events were still active.   </p>
<p>So I then thought of combining my previous attempt at preventing links and buttons from being clicked on with mouseChildren and mouseEnabled.  I ended up only setting mouseChildren and mouseEnabled to true if the application has focus, and after a small timer dalay of 500 miliseconds.   That gives me just enough time to click on the application any where without also clicking on a link or button and activating it.   So now I can click on the application to give it focus any place I like without firing off click events:</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;height:640px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">NativeApplication.<span style="color: #006600;">nativeApplication</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ACTIVATE</span>, handleAppActivate<span style="color: #66cc66;">&#41;</span>;<br />
NativeApplication.<span style="color: #006600;">nativeApplication</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">DEACTIVATE</span>, handleAppDiactivate<span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> timer:Timer;<br />
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> handleAppActivate<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// set timer to enable mouse events after short delay on app activation</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; timer = <span style="color: #000000; font-weight: bold;">new</span> Timer<span style="color: #66cc66;">&#40;</span>500<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; timer.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>TimerEvent.<span style="color: #006600;">COMPLETE</span>, enableAppEvents<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; timer.<span style="color: #0066CC;">start</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> handleAppDiactivate<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066CC;">this</span>.<span style="color: #006600;">mouseChildren</span> = <span style="color: #000000; font-weight: bold;">false</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066CC;">this</span>.<span style="color: #006600;">mouseEnabled</span> = <span style="color: #000000; font-weight: bold;">false</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<br />
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> enableAppEvents<span style="color: #66cc66;">&#40;</span>event:TimerEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;timer.<span style="color: #0066CC;">stop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;timer.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>TimerEvent.<span style="color: #006600;">COMPLETE</span>, enableAppEvents<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #808080; font-style: italic;">// now scrolling and mouse events work</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066CC;">this</span>.<span style="color: #006600;">mouseChildren</span> = <span style="color: #000000; font-weight: bold;">true</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066CC;">this</span>.<span style="color: #006600;">mouseEnabled</span> = <span style="color: #000000; font-weight: bold;">true</span>;<br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></tbody></table></div>
<p>That&#8217;s it, a simple solution that was probably sitting under my nose the whole time but I never thought of it.</p>
<p><strong>UPDATE</strong></p>
<p>Jonnie Hallman from Adobe posted a great article that expands the idea of handling throttling and events (mouse wheel for example).   </p>
<p><a href="http://www.adobe.com/devnet/air/flex/articles/framerate_throttling.html">http://www.adobe.com/devnet/air/flex/articles/framerate_throttling.html</a></p>
<p>- Mister</p>
<p><map name='google_ad_map_746_1452acacd61817fb'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/746?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_746_1452acacd61817fb' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=746&amp;url= http%3A%2F%2Fwww.thanksmister.com%2Findex.php%2Farchive%2Fpreventing-scrolling-mouse-events-out-of-focus-air-application%2F' /></p>]]></content:encoded>
			<wfw:commentRss>http://www.thanksmister.com/index.php/archive/preventing-scrolling-mouse-events-out-of-focus-air-application/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
