View

ArrowBase and wmode transparent on Flash

  1. package tools
  2. {
  3.         /**
  4.          * this class allows to bypass the wmode restriction for the @ character
  5.          * @author lcharpentier
  6.          */
  7.         import flash.events.KeyboardEvent;
  8.         import flash.events.TextEvent;
  9.         import flash.events.Event;
  10.         import flash.text.TextField;
  11.         import flash.ui.Keyboard;
  12.         import flash.display.Stage;
  13.         import Error;
  14.         public class  ArrowBaseConverter
  15.         {
  16.                 private static var arrObject:Array = new Array();
  17.                 private static var arrKey:Array = new Array();
  18.                 private static var bPower:Boolean = false;
  19.                 private static var bDetected:Boolean = false;
  20.                 private static var oTarget:TextField;
  21.                 /**
  22.                  * Static method used to watch a textfield object and detect an @ character
  23.                  * @param       text
  24.                  */
  25.                 public static function watch(text:TextField)
  26.                 {
  27.                         if (!bPower)
  28.                                 startConverter(text);
  29.                         if (!isMonitored(text))
  30.                         {
  31.                                 text.addEventListener(Event.CHANGE, handleChange);
  32.                                 arrObject.push(text);
  33.                         }
  34.                 }
  35.                 /**
  36.                  * Method used to stop the survey of a textField
  37.                  * @param       text
  38.                  * @return boolean
  39.                  */
  40.                 public static function release(text:TextField):Boolean
  41.                 {
  42.                         try{
  43.                                 if (isMonitored(text))
  44.                                 {
  45.                                         text.removeEventListener(Event.CHANGE, handleChange);
  46.                                         arrObject.splice(arrObject.indexOf(text), 1);
  47.                                         return true;
  48.                                 }
  49.                                 return false;
  50.                         }
  51.                         catch (err:Error)
  52.                         {
  53.                                 throw(“An error occured while trying to unregister a the ArrowBaseConverter :” + err.message);
  54.                         }
  55.                         finally
  56.                         {
  57.                                 return false;
  58.                         }
  59.                 }
  60.                 /**
  61.                  * Method used to check if a textfield is already under survey
  62.                  * @param       text
  63.                  * @return boolean
  64.                  */
  65.                 public static function isMonitored(text:TextField):Boolean
  66.                 {
  67.                         if (arrObject.indexOf(text)!=-1)
  68.                                 return true;
  69.                         else
  70.                                 return false;
  71.                 }
  72.                 /**
  73.                  * Method used to initialize the survey engine
  74.                  * @param       text
  75.                  */
  76.                 private static function startConverter(text:TextField):void
  77.                 {
  78.                         if (text.stage == null)
  79.                                 throw new ArgumentError(“the TextField has to be into the display list to use this Class”);
  80.                         else
  81.                         {
  82.                                 text.stage.addEventListener(KeyboardEvent.KEY_DOWN, handleKeydown)
  83.                                 text.stage.addEventListener(KeyboardEvent.KEY_DOWN, handleKeyUp)
  84.                         }
  85.                 }
  86.                 /**
  87.                  * Listener used to check on change on a textfield
  88.                  * @param       e
  89.                  */
  90.                 private static function handleChange(e:Event):void
  91.                 {
  92.                         oTarget = e.currentTarget as TextField;
  93.                         if (bDetected)
  94.                         {
  95.                                 oTarget.text = oTarget.text.slice(0, -1)+“@”;
  96.                                 bDetected = false;
  97.                         }
  98.                 }
  99.                 /**
  100.                  * Listeners used to detect an key press
  101.                  * @param       e
  102.                  */
  103.                 private static function handleKeydown(e:KeyboardEvent):void
  104.                 {
  105.                         trace(“key:”+detectArrowbase(e.keyCode, e.charCode));
  106.                         if(arrKey.length == 0 || (arrKey.length > 0 && e.keyCode != arrKey[arrKey.length-1].key))
  107.                         {
  108.                                 if (detectArrowbase(e.keyCode, e.charCode))
  109.                                         bDetected = true;
  110.                                 else
  111.                                 {
  112.                                         bDetected = false;
  113.                                         arrKey.push({ key:e.keyCode, char:e.charCode});
  114.                                 }
  115.                         }
  116.                 }
  117.                 /**
  118.                  * Listener used to detect any key press
  119.                  * @param       e
  120.                  */
  121.                 private static function handleKeyUp(e:KeyboardEvent):void
  122.                 {
  123.                         arrKey = arrKey.splice(arrKey.indexOf(e.keyCode), 1);
  124.                 }
  125.                 /**
  126.                  * Method used to recognize a key associations which match the @ character
  127.                  * @param       kCode
  128.                  * @param       cCode
  129.                  * @return bollean
  130.                  */
  131.                 private static function detectArrowbase(kCode:Number, cCode:Number):Boolean
  132.                 {
  133.                         //trace(kCode + ” | ” + cCode);
  134.                         //Mac @ key verification
  135.                         if (kCode == 50 && cCode == 64)
  136.                                 return true;
  137.                         //PC Azerty @ verification
  138.                         else if (arrKey.length > 0 && arrKey[arrKey.length1].key == 18 && arrKey[arrKey.length1].char == 0 && kCode == 48 && cCode == 224)
  139.                                 return true;
  140.                         else
  141.                                 return false;
  142.                 }
  143.         }
  144. }

You may also like