<?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>SwellJS - another big fish in the sea &#187; key handling</title>
	<atom:link href="http://blog.justswell.org/tag/key-handling/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.justswell.org</link>
	<description>well-minded javascript library</description>
	<lastBuildDate>Fri, 20 Aug 2010 20:48:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Cross browser key event handling with Javascript and Swell</title>
		<link>http://blog.justswell.org/key-event-handling-with-javascript/</link>
		<comments>http://blog.justswell.org/key-event-handling-with-javascript/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 13:04:22 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Event]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Swell]]></category>
		<category><![CDATA[crossbrowser]]></category>
		<category><![CDATA[key handling]]></category>
		<category><![CDATA[keyboard events]]></category>
		<category><![CDATA[keyboard listener]]></category>

		<guid isPermaLink="false">http://blog.justswell.org/?p=115</guid>
		<description><![CDATA[Handling keyboard events with Javascript is an extremely difficult task considering each browser discrepancies. One guy compiled all the differences into a single page that he adequately called &#8220;Javascript madness :  Keyboard events&#8220;, and from there you can clearly see the real madness Of course we have directly experienced the aforementioned problems and found out [...]]]></description>
			<content:encoded><![CDATA[<p>Handling keyboard events with Javascript is an extremely difficult task considering each browser discrepancies.</p>
<p>One guy compiled all the differences into a single page that he adequately called &#8220;<a href="http://unixpapa.com/js/key.html" target="_blank">Javascript madness :  Keyboard events</a>&#8220;, and from there you can clearly see the real madness <img src='http://blog.justswell.org/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>Of course we have directly experienced the aforementioned problems and found out pretty smart solutions for almost any browser.</p>
<p>The two major headaches were IE not firing keypress event on specific keys and Opera not able to prevent default behavior of keydown event.</p>
<h3>HTMLEvents magic or how to fix messy browser implementation</h3>
<p>The special keys encompass all the keys that don&#8217;t cause a character to appear, but execute a certain function.  Among these keys we can find shift, escape, pageDown, enter and so on.</p>
<p><strong>Some general caveats (courtesy of quirksmode) :</strong></p>
<ul>
<li>IE doesn&#8217;t fire the keypress event for delete, end, enter, escape, function keys, home, insert, pageUp/Down and tab.</li>
<li>Onkeypress, Safari gives weird keyCode values in the 63200 range for delete, end, function keys, home and pageUp.Down. The  onkeydown and -up values are normal.</li>
<li>Alt, Cmd, Ctrl and Shift cannot be detected on Mac, except in Opera. However, you can always use the altKey,    ctrlKey, and shiftKey properties.</li>
</ul>
<p>For getting rid of this weird behavior we are capturing special keys on keydown and use a static property to hold the keyname (keymapper) and keycode that we use when keypress event is fired.</p>
<p>As we didn&#8217;t want to hack our code too much we just simulate the keypress event on IE when the unsupported special keys are pressed.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Check if keypressed is a special key</span>
_specialKeyName <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getSpecialKeyName</span><span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">getKeyCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>_specialKeyName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">this</span>._specialKey <span style="color: #339933;">=</span> _specialKeyName<span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>._keyCode    <span style="color: #339933;">=</span>  e.<span style="color: #660066;">getKeyCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">// This is a special key, we know that IE will not fire</span>
    <span style="color: #006600; font-style: italic;">// keypress event, we'll do it for him :D</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>Swell.<span style="color: #660066;">Core</span>.<span style="color: #660066;">Browser</span>.<span style="color: #660066;">isIE</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">simulate</span><span style="color: #009900;">&#40;</span>o<span style="color: #339933;">,</span> <span style="color: #3366CC;">'keypress'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">/**
 * Fires programmatically a native DOM event
 * @function simulate
 *
 * @param {String|HTMLElement|Array} o the object to assign the handler to
 * @param {String} type event type : click, change, keypress, etc, never prepend &quot;on&quot; keyword as Swell does this job for you
 *
 * @see https://developer.mozilla.org/en/DOM/element.dispatchEvent
 * @see http://msdn.microsoft.com/en-us/library/ms536390(VS.85).aspx
*/</span>
simulate <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>o<span style="color: #339933;">,</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> _event<span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">// Testing if o is an object or a string</span>
    <span style="color: #006600; font-style: italic;">// add is just dealing with DOM events, and is not meant to</span>
    <span style="color: #006600; font-style: italic;">// handle custom events of Swell Objects</span>
    <span style="color: #006600; font-style: italic;">// as Swell.Core.Dom is not part of the core distribution &amp;lt;~20K :D</span>
    <span style="color: #006600; font-style: italic;">// We will use the good old document.getElementById for element retrieval</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>Swell.<span style="color: #660066;">Core</span>.<span style="color: #660066;">isString</span><span style="color: #009900;">&#40;</span>o<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        o <span style="color: #339933;">=</span> _getById<span style="color: #009900;">&#40;</span>o<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>Swell.<span style="color: #660066;">Core</span>.<span style="color: #660066;">isArray</span><span style="color: #009900;">&#40;</span>o<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// Loop on function</span>
        <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> n <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> n <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> o.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> n<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">simulate</span><span style="color: #009900;">&#40;</span>o<span style="color: #009900;">&#91;</span>n<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        o <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>o<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">createEventObject</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// IE</span>
        _event <span style="color: #339933;">=</span> document.<span style="color: #660066;">createEventObject</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">return</span> o.<span style="color: #660066;">fireEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'on'</span> <span style="color: #339933;">+</span> e<span style="color: #339933;">,</span> _event<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// All other browsers</span>
        _event <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>
        _event.<span style="color: #660066;">initEvent</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//event type,bubbling,cancellable</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #339933;">!</span>o.<span style="color: #660066;">dispatchEvent</span><span style="color: #009900;">&#40;</span>_event<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.justswell.org/key-event-handling-with-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

