package
{
import flash.events.*;
import flash.display.*;
import flash.text.*;
import com.tadSrc.tadsClasses.*;
public class DOMExEventHandling extends Sprite
{
private var textField:TextField = new TextField();
//These two Array vars below are just a little bit extra here, to help with the typing in this example
//in reality if you were to make some sort of typing program in this way,
//you would need to account for all the keyCodes, like curly brackets and everything else...
//In this case i've taken care of enter - 13 - and some others, because
//otherwise when you hit enter, nothing would happen, so i have to tell it to put a newline.
//This is because, from javascript, there will be no charCode property available, so we have to
//only use keyCodes
//eneter backslash shift slash tab ctrl alt comma period
private var allCodesToReplace:Array = [ 13, 220, 16, 191, 9, 17, 18, 188, 190 ];
private var codeReplacers:Array = [ "\n", "\\", "", "\/", " ", "", "", "\,", "\." ];
private var middleCircle:Sprite = new Sprite();
public function DOMExEventHandling()
{
//ADDED_TO_STAGE NOT JUST ADDED, ADDED would make DOMExSetup happen twice.
if (stage) DOMExSetup()
else
addEventListener(Event.ADDED_TO_STAGE, DOMExSetup, false, 0, true);
}
private function DOMExSetup(e:Event):void
{
middleCircle.graphics.lineStyle(1, 0x000000);
middleCircle.graphics.drawCircle(0,0,stage.stageWidth/3);
var squarer:Sprite = new Sprite();squarer.graphics.beginFill(0x00DD00);
squarer.graphics.drawRect(0,0,stage.stageWidth/2,stage.stageWidth/2);
middleCircle.addChild(squarer);
squarer.x = 0 - squarer.width/2;
squarer.y = 0 - squarer.height/2;
middleCircle.x = stage.stageWidth/2;
middleCircle.y = stage.stageHeight/2;
addChild(middleCircle);
textField.defaultTextFormat = new TextFormat("Arial", 25, 0x000000);
textField.wordWrap = true;
textField.multiline = true;
textField.selectable = false;
textField.autoSize = TextFieldAutoSize.LEFT;
textField.type = TextFieldType.DYNAMIC;
textField.width = stage.stageWidth - 5;
textField.height = stage.stageHeight - 5;
addChild(textField);
//set up the DOMExEventDispatcher for the document.onkeydown event, and we want it to give the keyCode value
var domexEventCatcher:DOMExEventDispatcher = new DOMExEventDispatcher("document.onkeydown", ["keyCode"]);
//now the document.onkeydown event in the wrapper will dispatch a DOMExEvent
//so we listen for it with this line
domexEventCatcher.addEventListener(DOMExEvent.DOMEX_EVENT, masterKeyboardHandler);
//second setup of a DOMExEvent, this time for the mouse
var domexMouseEventCatcher:DOMExEventDispatcher =
new DOMExEventDispatcher("document.onmousemove", ["clientX", "clientY"], 1100);
domexMouseEventCatcher.addEventListener(DOMExEvent.DOMEX_EVENT, moveMiddleCircle);
//we don't even need this stage keyboard listener when in the wrapper
//stage.addEventListener(KeyboardEvent.KEY_DOWN, masterKeyboardHandler);
stage.addEventListener(MouseEvent.MOUSE_MOVE, moveMiddleCircle);
removeEventListener(Event.ADDED_TO_STAGE, DOMExSetup);
}
private function masterKeyboardHandler(e:Event = null):void
{
//The Array used as the second param in DOMExEventDispatcher
//will be used to get the corrisponding values from the javascript event
//those values will make up the eventPropertiesArray given by the DOMEx event
var whatWasPressed:Number = (e as KeyboardEvent == null) ? e.eventPropertiesArray[0] : e.keyCode;
var toDisplay:String = String.fromCharCode(whatWasPressed);
for each (var n:Number in allCodesToReplace) {
if (whatWasPressed == n) toDisplay = codeReplacers[allCodesToReplace.indexOf(n)];
}
if (whatWasPressed != 8) {
textField.appendText(toDisplay);
} else {
textField.text = textField.text.substring(0, textField.text.length-1);
}
}
private function moveMiddleCircle(e:Event = null):void
{
middleCircle.rotation = (e as MouseEvent != null) ? mouseX : e.eventPropertiesArray[0];
//if we needed clientY, for this event, it is in eventPropertiesArray[1]
}
}
}