Simulate click event in AS3
Asked Answered
P

3

5

Is there any way to simulate a click event in AS3? I'm trying this:

element.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_DOWN, true, false));

But click event isn't trigger it.

Pedigree answered 23/9, 2011 at 10:6 Comment(1)
What element are you trying to simulate the click to?Malachi
L
22

If you are listening for MouseEvent.CLICK then dispatch MouseEvent.CLICK. You are now dispatching MouseEvent.MOUSE_DOWN

element.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
Ligan answered 23/9, 2011 at 10:16 Comment(3)
I am curious, is the dispatch handler of Flash PREVENTING the firing of the MOUSE_DOWN event?Braggart
@Braggart if you are listening for a MOUSE_DOWN event, Flash will not handle CLICK events, and vice versa. The MOUSE_DOWN event is fired when you click the mouse, the MOUSE_UP when you release then mouse. The CLICK event is equivalent to a MOUSE_DOWN followed by a MOUSE_UP. However, dispatching MOUSE_DOWN and MOUSE_UP separately will not fire a CLICK eventLigan
As per Adobe's documents they state it clearly ... "For a click event to occur, it must always follow this series of events in the order of occurrence: mouseDown event, then mouseUp. The target object must be identical for both of these events; otherwise the click event does not occur. Any number of other mouse events can occur at any time between the mouseDown or mouseUp events; the click event still occurs." but unfortunately I seem to up unable to affect it, so either the system is listening on ANOTHER event stream that it reacts to, or there is a bug.Braggart
E
4

You must dispatch a MouseEvent.CLICK event.

element.dispatchEvent(new MouseEvent(MouseEvent.CLICK, true, false));
Euterpe answered 23/9, 2011 at 10:10 Comment(0)
B
0

To simulate a CLICK event you need first to dispatch:

element.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_DOWN, true, false));

followed by a:

element.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_UP, true, false));

On the MOUSE_UP event the handler will then issue a click event (if the mouse is OVER the element, so you may need to set the mouse_x and mouse_y variables in the dispatched event.

Braggart answered 23/9, 2011 at 18:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.