0

我正在尝试在文本字段上手动分派事件,以便我可以通过代码间接操作它(例如,将光标放在给定的 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 中。

请帮忙!

谢谢,-丰富

4

1 回答 1

1

你有点倒退了。当您单击一个对象并调度一个 MouseEvent 时,它基本上是在告诉世界“嘿,我被点击了”。该事件是点击的结果。仅在对象上分派事件实际上不会导致对象被单击(尽管如果其他对象正在侦听对象上的事件,则分派事件会导致他们认为对象已被单击,而不管它是否实际上是点击与否)。

我不太确定你在这里做什么,除非它只是一个实验,但我认为你想要做的,而不是简单地在 tf2 上调度事件,是解释 tf2 内部会发生什么该事件,然后以编程方式进行。

因此,如果您正在浏览来自 tf1 的存储事件数组,并且您看到一个 CLICK,并且您知道单击 tf2 会使其获得焦点,那么您会希望以编程方式赋予 tf2 焦点。

编辑:

重读您的原始帖子,我看到您说您正在尝试将光标设置在特定的 x,y 坐标处。我想你可能想看看 TextField.getLineIndexAtPoint()、TextField.getCharIndexAtPoint() 和 TextField.setSelection()

于 2010-05-14T03:06:31.590 回答