我正在尝试在文本字段上手动调度事件,以便我可以通过代码间接操作它(例如,将光标放在给定的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中。
请帮忙!
谢谢, 富
答案 0 :(得分:1)
你有点倒退。当你点击一个对象并调度一个MouseEvent时,它基本上告诉世界“嘿,我点击了”。该事件是点击的结果。只是在对象上调度事件实际上并不会导致单击对象(尽管如果其他对象正在侦听对象上的事件,则调度事件会导致他们认为该对象被单击,无论是否实际点击过。)
我不太确定你在这里会发生什么,除非它只是一个实验,但我认为你想做的事情,而不是简单地在tf2上调度事件,就是将tf2内部发生的事情解释为该事件的结果,然后以编程方式执行。
因此,如果您正在查看来自tf1的存储事件数组,并且您看到了CLICK,并且您知道点击tf2会使其获得焦点,那么您可能希望以编程方式提供tf2焦点。
编辑:
重读原帖,我看到你说你正试图将光标设置在特定的x,y坐标上。我想你可能想看看TextField.getLineIndexAtPoint(),TextField.getCharIndexAtPoint()和TextField.setSelection()