Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to SIMULATE a mouse click using a mouse down and up events?

A previous question has gotten me thinking. Is it possible to simulate a MouseEvent.CLICK to get fired by just firing first a MOUSE_DOWN followed by a MOUSE_UP.

As per Adobe's doumentation. "... 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." http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/InteractiveObject.html#event:click

From my tests it shows that the CLICK event is NOT constructed from the ActionScript 3 event queue. Or is something wrong with the code?

See:

package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;

[SWF(backgroundColor="0xFFFFFF", frameRate="30", width="800", height="600")]

public class Test extends Sprite
{
    private var spr:Sprite = new Sprite();
    public function Test()
    {
        trace("Test()");
        this.addEventListener(Event.ADDED_TO_STAGE,init);
    }
    public function init(e:Event):void
    {
        trace("init()");
        spr.graphics.beginFill(0xFF0000);
        spr.graphics.drawRect(0,0,200,80);
        spr.graphics.endFill();
        addChild(spr);
        addEventListener(Event.ENTER_FRAME, onEnterFrame);
        spr.addEventListener(MouseEvent.CLICK, onClick);
    }
    private var tick:int = 0;
    private function onEnterFrame(e:Event):void
    {
        if (tick == 1) spr.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_DOWN,true,false));
        if (tick == 2) spr.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_UP,true,false));
        if (tick == 3) spr.dispatchEvent(new MouseEvent(MouseEvent.CLICK,true,false));
        tick++;
    }
    private function onClick(e:MouseEvent):void
    {
        trace("onClick() MouseEvent.CLICK dispatched"); 
    }
}
}

I should get TWO 'onClick()' events instead of one.

like image 565
eLouai Avatar asked Sep 24 '11 19:09

eLouai


People also ask

How do you simulate a mouse click?

You can right-click by holding down the left mouse button.

How do you simulate a click event?

Method 1: Using the click() method: The click() method is used to simulate a mouse click on an element. It fires the click event of the element on which it is called. The event bubbles up to elements higher in the document tree and fires their click events also.

How do you trigger a mouse up event?

jQuery mouseup() Method The mouseup event occurs when the left mouse button is released over the selected element. The mouseup() method triggers the mouseup event, or attaches a function to run when a mouseup event occurs. Tip: This method is often used together with the mousedown() method.

Is click a mouse event?

The MouseEvent interface represents events that occur due to the user interacting with a pointing device (such as a mouse). Common events using this interface include click , dblclick , mouseup , mousedown .


2 Answers

The reason you cannot is that all three types are created by the plugin, and are not dependent upon each other.

MouseEvent.MOUSE_DOWN is fired as soon as you press on an interactive object. MouseEvent.MOUSE_UP is fired as soon as you release the mouse, it does not depend upon the mouse click having started a MOUSE_DOWN within the same InteractiveObject. MouseEvent.CLICK will only be fired when both events take place in the same object without the cursor leaving the object between the itnerval of mouse down and mouse up.

So you can see that there is a simple case, or two even, in which both MOUSE_DOWN and MOUSE_UP are fired, but CLICK is not called because the event is not a CLICK.

Furthermore, the ability to simply dispatch a MouseEvent.CLICK event makes it unnecesasry to create fake user interaction by firing multiple mouse events, when only one is called for. It would be awkward to have to track each object clicked in the mouse down listener, in order to check that the moue up should also trigger a click. You would also need to remove references when a ROLL_OUT event happened.

In short, the CLICK event is really a lot more than the MOUSE_DOWN and MOUSE_UP events. It is also managing that the events happened in succession and on the same display object without leaving the object. When that happens, and ONLY when that happens flash dispatches a CLICK event.

like image 167
Plastic Sturgeon Avatar answered Sep 21 '22 14:09

Plastic Sturgeon


it's impossible, this code:

package 
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.events.MouseEvent;

    /**
     * ...
     * @author www0z0k
     */
    public class Main extends Sprite {

        public function Main():void {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            stage.addEventListener(MouseEvent.CLICK, onClick);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, onStageKeyDown);
        }

        private function onStageKeyDown(e:KeyboardEvent):void {
            trace('pressed');
            stage.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_DOWN));
            stage.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_UP));           
            stage.dispatchEvent(new MouseEvent(MouseEvent.CLICK));          
        }

        private function onClick(e:MouseEvent):void{
            trace('clicked');
        }       
    }   
}

outputs just:

pressed
clicked

after a key is pressed on the keyboard

like image 27
www0z0k Avatar answered Sep 21 '22 14:09

www0z0k