<?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>indieas &#187; onscreen</title>
	<atom:link href="http://www.indieas.org/tag/onscreen/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.indieas.org</link>
	<description>independent actionscript developer team</description>
	<lastBuildDate>Tue, 19 Oct 2010 13:43:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Onscreen Keyboard with AIR</title>
		<link>http://www.indieas.org/2009/11/onscreen-keyboard-with-air/</link>
		<comments>http://www.indieas.org/2009/11/onscreen-keyboard-with-air/#comments</comments>
		<pubDate>Sat, 14 Nov 2009 18:20:20 +0000</pubDate>
		<dc:creator>mich</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[air]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[htmlloader]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[keyboard]]></category>
		<category><![CDATA[onscreen]]></category>
		<category><![CDATA[screen]]></category>
		<category><![CDATA[textarea]]></category>
		<category><![CDATA[textfield]]></category>
		<category><![CDATA[touchschreen]]></category>

		<guid isPermaLink="false">http://www.indieas.org/?p=158</guid>
		<description><![CDATA[In one of my last projects I had to develop an air application with an onscreen keyboard that is working with flash as with the internal html browser of air. I realized pretty quickly that this isn&#8217;t that easy as it seams. There are multiple problems especially with the html view: how do i get [...]]]></description>
			<content:encoded><![CDATA[<p>In one of my last projects I had to develop an air application with an <strong>onscreen keyboard</strong> that is working with <strong>flash</strong> as with the <strong>internal html browser</strong> of air.</p>
<p>I realized pretty quickly that this isn&#8217;t that easy as it seams. There are multiple problems especially with the html view:</p>
<ul>
<li>how do i get the focus in the html view to add letters.</li>
<li>how do i get the selection to add chars on the right position.</li>
<li>how do i know when a user clicks and inputfield/textarea.</li>
<li>how do i prevent that flash do set the focus on the keyboard when I click on a letter sprite.</li>
</ul>
<p>After some experiments I discovered the really nice javascript support of the internal browser. So my first problem was already gone, when I found the javascript command:</p>
<pre class="brush: as3; title: ; notranslate">
var webView:HTMLLoader = new HTMLLoader();
//...later, to get the element with the focus call:
var element:Object = webView.window.document.activeElement;
</pre>
<p>I wasn&#8217;t really into javascript so I was surprised to discover even more possibilities. Now with the focus I can place an onscreen keyboard made with flash and get the activeElement of the htmlloader and add characters to it. something like this:</p>
<pre class="brush: as3; title: ; notranslate">
var inputField:Object = webView.window.document.activeElement;
if( inputField != null ) {
   var select:Number = addScreenBoardText( e.char, inputField, &quot;value&quot;, &quot;selectionStart&quot;, &quot;selectionEnd&quot; );
   inputField.setSelectionRange( select, select );
}
</pre>
<p>The function where I add the char and return the selection where the cursor should be set:</p>
<pre class="brush: as3; title: ; notranslate">
private function addScreenBoardText( insert:String, object:Object, textAttribute:String, selectionStartAttribute:String, selectionEndAttribute:String ):Number {
  var selectionStart:Number = object[ selectionStartAttribute ];
  var selectionEnd:Number = object[ selectionEndAttribute ];
  var original:String = object[ textAttribute ];
  var newText:String = original.substring( 0, selectionStart ) + insert + original.substr( selectionEnd );
  object[ textAttribute ] = newText;
  return selectionStart + insert.length;
}
</pre>
<p>Why I make this weird looking function is because I can use it also for the flash TextField just with different parameters. So now we can add letters to a textfield in a html view as a flash TextField. But one big problem is that we loose the focus every time we click on our on screen keyboard. And there is a way to prevent the change of the focus which is very handy in this situation:</p>
<pre class="brush: as3; title: ; notranslate"> // in our main application class we listen to the mouse focus change event.
addEventListener( FocusEvent.MOUSE_FOCUS_CHANGE, onMouseFocus );

// in the listener we check if the relatedObject is a Key,Screenboard ( these are my classes for the Keyboard that get mouseclicks )
// then we prevent the default behaviour of the event. which means no focus change is triggered.
private function onMouseFocus( fe:FocusEvent ):void {
if( fe.relatedObject is Key || fe.relatedObject is ScreenBoard ) fe.preventDefault();
}
</pre>
<p>We saw earlier that is was possible to write to javascript objects as normal in actionscript. I was even more surprised as its possible to listen to javascript events with actionscript functions. That allows me to listen to javascript focus changes and hide/show the keyboard. This way I can make an overlay keyboard that is only visible when I click in a textfield which I find is a very nice solution. So this is how I listened to the javascript events ( keep in mind that this is all actionscript ):</p>
<pre class="brush: as3; title: ; notranslate">
// register everytime the page has loaded!
private function onPageLoaded( e:Event ):void {
  webView.window.addEventListener( &quot;focus&quot;, onJavaScriptFocusIn, true );
  webView.window.addEventListener( &quot;blur&quot;, onJavaScriptFocusOut, true );
 }

// if we get a focus in event from js, check if its a inputfield or a textarea. then show the keyboard.
private function onJavaScriptFocusIn( e:Object ):void {
  if( e.target.localName == &quot;input&quot; || e.target.localName == &quot;textarea&quot; ) {
    dispatchEvent( new JavaScriptEvent( JavaScriptEvent.FOCUS_IN ) );
  }
}

// we lost focus - hide keyboard
private function onJavaScriptFocusOut( e:Object ):void {
  dispatchEvent( new JavaScriptEvent( JavaScriptEvent.FOCUS_OUT ) );
}
</pre>
<p>With all this you can build your onscreen keyboard that is working in <strong>flash as in html</strong>. I made a<a href="http://www.indieas.org/wp-content/uploads/2009/11/screenboard.zip"> quick flexbuilder air project</a> that you can download with a keyboard, textfield and htmlloader.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.indieas.org/2009/11/onscreen-keyboard-with-air/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

