Flashでの入力TextFieldの不具合の対処:Javascript promptと連携例

Mac版、chrome にて、InputタイプのTextFieldに日本語入力ができないみたいなので、
JSのPromptと連携して文字入力させるテスト。


例は特にChrome+Mac制限はしてない。

WonderflはExternalInterfaceがつかえない?みたいなので断念。
テキストフィールドをクリックして入力する流れで↓

This movie requires Flash Player 9

あらかじめHTMLにjqueryが必要 (downloadファイルにはgoogleapiのをinclude済)

http://blog.quq.jp/files/tfjsTextInput/tfjsTextInput.zip

そもそも、Flash側で直せる方法あったらおしえてください。

Actionscript:
  1. package {
  2.    
  3.     /**
  4.      * インポート
  5.      */
  6.     import flash.events.Event;
  7.     import flash.events.FocusEvent;
  8.     import flash.events.IEventDispatcher;
  9.     import flash.external.ExternalInterface;
  10.     import flash.display.Sprite;
  11.     import flash.text.TextField;
  12.     import flash.text.TextFieldType;
  13.     import flash.utils.*;
  14.     import flash.xml.XMLDocument;
  15.     import flash.system.Security;
  16.     import flash.system.Capabilities;
  17.     import flash.system.IME;
  18.     import flash.system.IMEConversionMode;
  19.    
  20.     /**
  21.      * MacChromeで日本語入力ができないみたいなので
  22.      * JSのPromptで文字入力をさせる案
  23.      * ※ 例はとくにChrome制限とはかけていない
  24.      */
  25.     public class FlashTest extends Sprite {
  26.                
  27.         //ユニークID
  28.         protected var flashid:String = "abcd12345";
  29.                
  30.         //入力フィールド
  31.         protected var inputtf:TextField = new TextField();
  32.                
  33.         /**
  34.          * コンストラクタ
  35.          */
  36.         public function FlashTest() {
  37.             this.addEventListener(Event.ADDED_TO_STAGE, onAdded);
  38.         }
  39.         //
  40.         protected function onAdded(e:Event):void{
  41.                    
  42.             IEventDispatcher(e.target).removeEventListener(e.type, arguments.callee);
  43.                    
  44.             Security.allowDomain(ExternalInterface.call("function() { return location.hostname }"));
  45.                    
  46.            
  47.             //TextFieldを準備
  48.             var label:TextField = new TextField();
  49.             label.text = "入力フィールド:";
  50.             this.addChild(label);
  51.             this.stage.focus = label;
  52.             label.x = 100;
  53.             label.y = 80;
  54.            
  55.             inputtf.text = "";
  56.             inputtf.width = 200;
  57.             inputtf.height = 30;
  58.             inputtf.x = 100;
  59.             inputtf.y = 100;
  60.             inputtf.border = true;
  61.             inputtf.type = TextFieldType.INPUT;
  62.             inputtf.addEventListener(FocusEvent.FOCUS_IN, tfCatchFucus);
  63.             this.addChild(inputtf);
  64.            
  65.             //JSのセットアップ
  66.            
  67.                 var xmldata:XML = new XML(<xml><![CDATA[
  68.                                
  69.                     function() {
  70.                                
  71.                         document.myflashid = '##flashide##';
  72.                         document.swftarget;
  73.                                
  74.                         //Flashへ接続テスト
  75.                         //※あらかじめhtmlにjqueryの導入が必要
  76.                         document.connectSWF = function(){
  77.                             $("object").each(function() {
  78.                             //flashide値がマッチするdomを探す
  79.                             try{
  80.                                 if (this.connect(document.myflashid)) {
  81.                                     document.swftarget = this;
  82.                                 }
  83.                             }catch(e){}                    
  84.                             });    
  85.                         }  
  86.                                
  87.                         document.openPrompt = function(defaultstr) {
  88.                             var result = window.prompt("入力してください。", defaultstr);
  89.                             result = !result ? "" : result;
  90.                             //flashに通知
  91.                             if(document.swftarget){
  92.                                 document.swftarget.update(result);
  93.                             }
  94.                         }
  95.                                
  96.                     }
  97.                            
  98.                 ]]></xml>);
  99.                        
  100.                 //JSの書き込み
  101.                 var xmlstr:String = String(xmldata);
  102.                 xmlstr = xmlstr.split('##flashide##').join(this.flashid);
  103.                 ExternalInterface.call(xmlstr);
  104.                        
  105.                 //callback登録
  106.                 ExternalInterface.addCallback("connect", onConnectFromJS);
  107.                 ExternalInterface.addCallback("update", update);
  108.                        
  109.             //接続
  110.             ExternalInterface.call("document.connectSWF");
  111.                    
  112.         }  
  113.                
  114.         /**
  115.          * TextFieldのフォーカスの取得
  116.          */
  117.         protected function tfCatchFucus(e:FocusEvent):void {
  118.             openJsPrompt();
  119.         }
  120.            
  121.         /**
  122.          * JSからのConnect
  123.          */
  124.         protected function onConnectFromJS(flashid:String):Boolean{
  125.             if (this.flashid == flashid) {
  126.                 return true;
  127.             }else {
  128.                 return false;
  129.             }
  130.         }
  131.                
  132.         /**
  133.          * JS Promptを表示する
  134.          */
  135.         protected function openJsPrompt():void {
  136.             //IME日本語入力をONにする
  137.             if (Capabilities.hasIME)
  138.             {
  139.                 try
  140.                 {
  141.                     IME.enabled = true;
  142.                     IME.conversionMode = IMEConversionMode.JAPANESE_HIRAGANA ;  
  143.                 }
  144.                 catch (error:Error){}
  145.             }
  146.             ExternalInterface.call("document.openPrompt", [inputtf.text]);
  147.         }
  148.        
  149.         /**
  150.          * 更新
  151.          */
  152.         protected function update(str:String):void{
  153.             inputtf.text = str;
  154.            
  155.             //フォーカスを外す
  156.             var s:FlashTest = this;
  157.             setTimeout(function(){
  158.                 s.stage.focus = null;
  159.             },1);
  160.         }
  161.            
  162.     }
  163. }