可以通过dispatchEvent()操作UI元素吗?

发布于 2024-09-02 03:49:53 字数 6043 浏览 7 评论 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 中。

请帮忙!

谢谢, -富有的

I'm trying to manually dispatch events on a textfield so I can manipulate it indirectly via code (e.g. place cursor at a given set of x/y coordinates). However, my events seem to have no effect.

I've written a test to experiment with this phenomenon:

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);
        }
    }
}

Essentially, all this test does is to use setTimeout to effectively 'record' events on TextField tf and replay them five seconds later on TextField tf2. When an event is dispatched on tf2, it is traced to the console output. The console output upon running this program and clicking on tf is:

[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]

As we can see, the events are being captured and replayed successfully. However, no change occurs in tf2 - the mouse cursor does not appear in tf2 as we would expect. In fact, the cursor remains in tf even after the tf2 events are dispatched.

Please help!

Thanks,
-Rich

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜血缘 2024-09-09 03:49:53

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

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

因此,如果您正在浏览 tf1 中存储的事件数组,并且看到单击,并且您知道单击 tf2 会为其提供焦点,那么您可能希望以编程方式为 tf2 提供焦点。

编辑:

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

You're sort of going backwards. When you click on an object and it dispatches a MouseEvent, it's basically telling the world "hey I got clicked on". The event is a consequence of the click. Just dispatching the event on the object does not actually cause the object to be clicked on (though if other objects are listening for events on the object, dispatching the event would cause them to think that the object was clicked, regardless of whether it was actually clicked on or not).

I'm not quite sure what you're going for here, unless it's just an experiment, but I think what you want to do, instead of simply dispatching the event on tf2, is interpret what would have happened inside tf2 as a result of that event, and then do that programmatically.

So if you're going through your array of stored events from tf1, and you see a CLICK, and you know that clicking on tf2 would give it focus, you would want to programmatically give tf2 focus.

Edit:

Rereading your original post, I see you said you're trying to set the cursor at a particular x,y coordinate. I think you might want to take a look at TextField.getLineIndexAtPoint(), TextField.getCharIndexAtPoint(), and TextField.setSelection()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文