<?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>techno.blog(&#34;Dion&#34;) &#187; w3c events</title>
	<atom:link href="http://almaer.com/blog/tag/w3c-events/feed" rel="self" type="application/rss+xml" />
	<link>http://almaer.com/blog</link>
	<description>blogging about life, the universe, and everything tech</description>
	<lastBuildDate>Tue, 28 Aug 2012 14:41:55 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Dealing with W3C Events; A story of running around in circles</title>
		<link>http://almaer.com/blog/dealing-with-w3c-events-a-story-of-running-around-in-circles</link>
		<comments>http://almaer.com/blog/dealing-with-w3c-events-a-story-of-running-around-in-circles#comments</comments>
		<pubDate>Fri, 04 Jul 2008 16:34:52 +0000</pubDate>
		<dc:creator>dion</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[tip]]></category>
		<category><![CDATA[w3c events]]></category>

		<guid isPermaLink="false">http://almaer.com/blog/dealing-with-w3c-events-a-story-of-running-around-in-circles</guid>
		<description><![CDATA[I am working on an interesting pet project that has a fairly rich UI. A rich UI means dealing with events, and I had a wake up call on what a pain it can be to deal with in the browser world.
I ended up on a wild goose chase that ended up having a simple [...]]]></description>
			<content:encoded><![CDATA[<p>I am working on an interesting pet project that has a fairly rich UI. A rich UI means dealing with events, and I had a wake up call on what a pain it can be to deal with in the browser world.</p>
<p>I ended up on a wild goose chase that ended up having a simple solution.</p>
<p>Imagine one section of an application that is a canvas element, and another piece that is a <code>input type="text"</code>. I wanted a way to jump between the two with a keystroke, e.g. Ctrl-J for jump.</p>
<p>Jumping from the canvas element to textbox was fine, but it wouldn&#8217;t work on the way back. I was trying to <code>$('canvas').focus()</code> but it wouldn&#8217;t work.</p>
<p><b>initMouseEvent</b></p>
<p>I stupidly thought that I should just simulate the click on the canvas element, since that was working just fine for me.</p>
<p>We are pretty fortunate that the ability to kick off true events programatically is bound into the event model itself.</p>
<p>This means that I could:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">createEvent</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #003366; font-weight: bold;">var</span> evt <span style="color: #339933;">=</span> document.<span style="color: #660066;">createEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'MouseEvents'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   evt.<span style="color: #660066;">initMouseEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span>
            <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>     <span style="color: #006600; font-style: italic;">// Click events bubble</span>
            <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>     <span style="color: #006600; font-style: italic;">// and they can be cancelled</span>
            document.<span style="color: #660066;">defaultView</span><span style="color: #339933;">,</span>  <span style="color: #006600; font-style: italic;">// Use the default view</span>
            <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span>        <span style="color: #006600; font-style: italic;">// Just a single click</span>
            <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>        <span style="color: #006600; font-style: italic;">// Don't bother with coordinates</span>
            <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>
            <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>
            <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>
            <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>    <span style="color: #006600; font-style: italic;">// Don't apply any key modifiers</span>
            <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
            <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
            <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
            <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>        <span style="color: #006600; font-style: italic;">// 0 - left, 1 - middle, 2 - right</span>
            <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #006600; font-style: italic;">// Click events don't have any targets other than</span>
                      <span style="color: #006600; font-style: italic;">// the recipient of the click</span>
 $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'canvas'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">dispatchEvent</span><span style="color: #009900;">&#40;</span>evt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This only slightly worked. I could test that the click was going through. I would add an <code>onclick</code> handler to <code>canvas</code> and it would fire just fine. However, something weird was happening. The focus was still in the textbox even with this click happening (which didn&#8217;t happen when I actually clicked on the darn thing).</p>
<p><b>Events from within events</b></p>
<p>Since the click was working, why not just force the blur on the textbox?</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> events <span style="color: #339933;">=</span> document.<span style="color: #660066;">createEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'HTMLEvents'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066;">blur</span> <span style="color: #339933;">=</span> events.<span style="color: #660066;">initEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'blur'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
his.<span style="color: #660066;">dispatchEvent</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">blur</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>But that got me:</p>
<blockquote><p>
Component returned failure code: 0&#215;80070057 (NS_ERROR_ILLEGAL_VALUE) [nsIDOMEventTarget.dispatchEvent.....]
</p></blockquote>
<p>Hmm, on wait. This was happening from within an <code>onkeypress</code> handler and it appears that you can&#8217;t do this in a nested way. A <code>setTimeout(..)</code> to push it off until later didn&#8217;t make it any happier either.</p>
<p><b>tabindex=0</b></p>
<p>As I looked at this mess, I quickly realised that I had been unravelling a string, and I should take a step back.</p>
<p>I talked to Alex Russell, and he immediately said &#8220;erm, and you made sure that tabindex was set on the canvas element?&#8221;</p>
<p>Doh, it wasn&#8217;t. This is why the focus method (which did exist) didn&#8217;t do anything. I quickly added <code>tabindex=-1</code> and now I could simply <code>$('canvas').focus()</code> and lo-and-behold it would focus!</p>
<p><b>Event.stop(e)</b></p>
<p>Great, the home stretch. I took the test and put it on the live code and it&#8230; still didn&#8217;t quite work. God damn it.</p>
<p>I took another step back.</p>
<p>A global key handler was set via <code>document.onkeydown</code> that was handling the world. When it saw a Ctrl-J it would short circuit and give focus to the textbox. The textbox has its own <code>onkeydown</code> that would do the opposite. A global flag held the state of global/textbox.</p>
<p>This is the problem. I had mucked up the event propogation, so in the case of jumping from textbox to canvas it would first go through the textbox handler, but after it was done it would STILL run the global handler which would kick it right back!</p>
<p>I just needed to <code>e.stopProgagation()</code>, or even better <a href="http://www.prototypejs.org/api/event/stop">Event.stop(e)</a> to stop any default action too.</p>
<p>Phew, all that running around to get back to square one. Now it works, and it reminded me of how the simplest things can be tricky.</p>
]]></content:encoded>
			<wfw:commentRss>http://almaer.com/blog/dealing-with-w3c-events-a-story-of-running-around-in-circles/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
