我正在尝试在文本字段上手动分派事件,以便我可以通过代码间接操作它(例如,将光标放在给定的 x/y 坐标集上)。但是,我的事件似乎没有效果。
我写了一个测试来试验这种现象:
package sandbox {
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.TextFieldAutoSize;
import flash.utils.setTimeout;
public class Test extends Sprite {
private var tf:TextField;
private var tf2:TextField;
public function Test() {
super();
tf = new TextField();
tf.text = 'Interact here';
tf.type = TextFieldType.INPUT;
addChild(tf);
tf2 = new TextField();
tf2.text = 'Same events replayed with five second delay here';
tf2.autoSize = TextFieldAutoSize.LEFT;
tf2.type = TextFieldType.INPUT;
tf2.y = 30;
addChild(tf2);
tf.addEventListener(MouseEvent.CLICK, mouseListener);
tf.addEventListener(MouseEvent.DOUBLE_CLICK, mouseListener);
tf.addEventListener(MouseEvent.MOUSE_DOWN, mouseListener);
tf.addEventListener(MouseEvent.MOUSE_MOVE, mouseListener);
tf.addEventListener(MouseEvent.MOUSE_OUT, mouseListener);
tf.addEventListener(MouseEvent.MOUSE_OVER, mouseListener);
tf.addEventListener(MouseEvent.MOUSE_UP, mouseListener);
tf.addEventListener(MouseEvent.MOUSE_WHEEL, mouseListener);
tf.addEventListener(MouseEvent.ROLL_OUT, mouseListener);
tf.addEventListener(MouseEvent.ROLL_OVER, mouseListener);
}
private function mouseListener(event:MouseEvent):void {
//trace(event);
setTimeout(function():void {trace(event); tf2.dispatchEvent(event);}, 5000);
}
}
}
本质上,这个测试所做的只是使用 setTimeout 在 TextField tf 上有效地“记录”事件,并在五秒钟后在 TextField tf2 上重放它们。当在 tf2 上调度事件时,它会被跟踪到控制台输出。运行此程序并单击 tf 时的控制台输出为:
[MouseEvent type="mouseMove" bubbles=true cancelable=false eventPhase=3 localX=0 localY=1 stageX=0 stageY=1 relatedObject=null ctrlKey=false altKey=false shiftKey=false delta=0]
[MouseEvent type="rollOver" bubbles=false cancelable=false eventPhase=2 localX=0 localY=1 stageX=0 stageY=1 relatedObject=null ctrlKey=false altKey=false shiftKey=false delta=0]
[MouseEvent type="mouseOver" bubbles=true cancelable=false eventPhase=3 localX=0 localY=1 stageX=0 stageY=1 relatedObject=null ctrlKey=false altKey=false shiftKey=false delta=0]
[MouseEvent type="mouseMove" bubbles=true cancelable=false eventPhase=3 localX=2 localY=1 stageX=2 stageY=1 relatedObject=null ctrlKey=false altKey=false shiftKey=false delta=0]
[MouseEvent type="mouseMove" bubbles=true cancelable=false eventPhase=3 localX=2 localY=2 stageX=2 stageY=2 relatedObject=null ctrlKey=false altKey=false shiftKey=false delta=0]
[MouseEvent type="mouseMove" bubbles=true cancelable=false eventPhase=3 localX=2 localY=3 stageX=2 stageY=3 relatedObject=null ctrlKey=false altKey=false shiftKey=false delta=0]
[MouseEvent type="mouseMove" bubbles=true cancelable=false eventPhase=3 localX=3 localY=3 stageX=3 stageY=3 relatedObject=null ctrlKey=false altKey=false shiftKey=false delta=0]
[MouseEvent type="mouseMove" bubbles=true cancelable=false eventPhase=3 localX=5 localY=3 stageX=5 stageY=3 relatedObject=null ctrlKey=false altKey=false shiftKey=false delta=0]
[MouseEvent type="mouseMove" bubbles=true cancelable=false eventPhase=3 localX=6 localY=5 stageX=6 stageY=5 relatedObject=null ctrlKey=false altKey=false shiftKey=false delta=0]
[MouseEvent type="mouseMove" bubbles=true cancelable=false eventPhase=3 localX=7 localY=5 stageX=7 stageY=5 relatedObject=null ctrlKey=false altKey=false shiftKey=false delta=0]
[MouseEvent type="mouseMove" bubbles=true cancelable=false eventPhase=3 localX=9 localY=5 stageX=9 stageY=5 relatedObject=null ctrlKey=false altKey=false shiftKey=false delta=0]
[MouseEvent type="mouseMove" bubbles=true cancelable=false eventPhase=3 localX=10 localY=5 stageX=10 stageY=5 relatedObject=null ctrlKey=false altKey=false shiftKey=false delta=0]
[MouseEvent type="mouseMove" bubbles=true cancelable=false eventPhase=3 localX=11 localY=5 stageX=11 stageY=5 relatedObject=null ctrlKey=false altKey=false shiftKey=false delta=0]
[MouseEvent type="mouseMove" bubbles=true cancelable=false eventPhase=3 localX=12 localY=5 stageX=12 stageY=5 relatedObject=null ctrlKey=false altKey=false shiftKey=false delta=0]
[MouseEvent type="mouseDown" bubbles=true cancelable=false eventPhase=3 localX=12 localY=5 stageX=12 stageY=5 relatedObject=null ctrlKey=false altKey=false shiftKey=false delta=0]
[MouseEvent type="mouseUp" bubbles=true cancelable=false eventPhase=3 localX=12 localY=5 stageX=12 stageY=5 relatedObject=null ctrlKey=false altKey=false shiftKey=false delta=0]
[MouseEvent type="click" bubbles=true cancelable=false eventPhase=3 localX=12 localY=5 stageX=12 stageY=5 relatedObject=null ctrlKey=false altKey=false shiftKey=false delta=0]
[MouseEvent type="mouseMove" bubbles=true cancelable=false eventPhase=3 localX=10 localY=4 stageX=10 stageY=4 relatedObject=null ctrlKey=false altKey=false shiftKey=false delta=0]
[MouseEvent type="mouseMove" bubbles=true cancelable=false eventPhase=3 localX=9 localY=2 stageX=9 stageY=2 relatedObject=null ctrlKey=false altKey=false shiftKey=false delta=0]
[MouseEvent type="mouseMove" bubbles=true cancelable=false eventPhase=3 localX=9 localY=1 stageX=9 stageY=1 relatedObject=null ctrlKey=false altKey=false shiftKey=false delta=0]
[MouseEvent type="mouseOut" bubbles=true cancelable=false eventPhase=3 localX=-1 localY=-1 stageX=-1 stageY=-1 relatedObject=null ctrlKey=false altKey=false shiftKey=false delta=0]
[MouseEvent type="rollOut" bubbles=false cancelable=false eventPhase=2 localX=-1 localY=-1 stageX=-1 stageY=-1 relatedObject=null ctrlKey=false altKey=false shiftKey=false delta=0]
正如我们所见,事件被成功捕获和重放。但是,tf2 中没有发生任何变化——鼠标光标没有像我们预期的那样出现在 tf2 中。事实上,即使在调度了 tf2 事件之后,光标仍保留在 tf 中。
请帮忙!
谢谢,-丰富