views:

87

answers:

1

Hi all!

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

+1  A: 

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()

krichard
thanks mucho! :)