Tags: flash,AS3

Sort by: Date / Title /

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

First / Previous / Next / Last / Page 1 of 1 (1 posteets)