-
package tools
-
{
-
-
/**
-
* this class allows to bypass the wmode restriction for the @ character
-
* @author lcharpentier
-
*/
-
-
import flash.events.KeyboardEvent;
-
import flash.events.TextEvent;
-
import flash.events.Event;
-
import flash.text.TextField;
-
import flash.ui.Keyboard;
-
import flash.display.Stage;
-
import Error;
-
-
public class ArrowBaseConverter
-
{
-
private static var arrObject:Array = new Array();
-
private static var arrKey:Array = new Array();
-
private static var bPower:Boolean = false;
-
private static var bDetected:Boolean = false;
-
private static var oTarget:TextField;
-
-
/**
-
* Static method used to watch a textfield object and detect an @ character
-
* @param text
-
*/
-
public static function watch(text:TextField)
-
{
-
if (!bPower)
-
startConverter(text);
-
if (!isMonitored(text))
-
{
-
text.addEventListener(Event.CHANGE, handleChange);
-
arrObject.push(text);
-
}
-
}
-
-
/**
-
* Method used to stop the survey of a textField
-
* @param text
-
* @return boolean
-
*/
-
public static function release(text:TextField):Boolean
-
{
-
try{
-
if (isMonitored(text))
-
{
-
text.removeEventListener(Event.CHANGE, handleChange);
-
arrObject.splice(arrObject.indexOf(text), 1);
-
return true;
-
}
-
return false;
-
}
-
catch (err:Error)
-
{
-
throw(“An error occured while trying to unregister a the ArrowBaseConverter :” + err.message);
-
}
-
finally
-
{
-
return false;
-
}
-
}
-
-
-
/**
-
* Method used to check if a textfield is already under survey
-
* @param text
-
* @return boolean
-
*/
-
public static function isMonitored(text:TextField):Boolean
-
{
-
-
if (arrObject.indexOf(text)!=-1)
-
return true;
-
else
-
return false;
-
}
-
-
/**
-
* Method used to initialize the survey engine
-
* @param text
-
*/
-
private static function startConverter(text:TextField):void
-
{
-
if (text.stage == null)
-
throw new ArgumentError(“the TextField has to be into the display list to use this Class”);
-
else
-
{
-
text.stage.addEventListener(KeyboardEvent.KEY_DOWN, handleKeydown)
-
text.stage.addEventListener(KeyboardEvent.KEY_DOWN, handleKeyUp)
-
}
-
}
-
-
/**
-
* Listener used to check on change on a textfield
-
* @param e
-
*/
-
private static function handleChange(e:Event):void
-
{
-
oTarget = e.currentTarget as TextField;
-
-
if (bDetected)
-
{
-
oTarget.text = oTarget.text.slice(0, -1)+“@”;
-
bDetected = false;
-
}
-
}
-
-
/**
-
* Listeners used to detect an key press
-
* @param e
-
*/
-
private static function handleKeydown(e:KeyboardEvent):void
-
{
-
trace(“key:”+detectArrowbase(e.keyCode, e.charCode));
-
if(arrKey.length == 0 || (arrKey.length > 0 && e.keyCode != arrKey[arrKey.length-1].key))
-
{
-
if (detectArrowbase(e.keyCode, e.charCode))
-
bDetected = true;
-
else
-
{
-
bDetected = false;
-
arrKey.push({ key:e.keyCode, char:e.charCode});
-
}
-
}
-
}
-
-
/**
-
* Listener used to detect any key press
-
* @param e
-
*/
-
private static function handleKeyUp(e:KeyboardEvent):void
-
{
-
arrKey = arrKey.splice(arrKey.indexOf(e.keyCode), 1);
-
}
-
-
/**
-
* Method used to recognize a key associations which match the @ character
-
* @param kCode
-
* @param cCode
-
* @return bollean
-
*/
-
private static function detectArrowbase(kCode:Number, cCode:Number):Boolean
-
{
-
//trace(kCode + ” | ” + cCode);
-
//Mac @ key verification
-
if (kCode == 50 && cCode == 64)
-
return true;
-
//PC Azerty @ verification
-
else if (arrKey.length > 0 && arrKey[arrKey.length – 1].key == 18 && arrKey[arrKey.length – 1].char == 0 && kCode == 48 && cCode == 224)
-
return true;
-
else
-
return false;
-
}
-
-
-
}
-
-
}