Stefan Woskowiak
Stefan Woskowiak

Reputation: 115

Simulate mouse click AS3

I'm having trouble simulating a click call to a button(displayObject) thats generated via an API call( youtube as3 API). I have not seen any mention of security reasons as to why I can not simulate a click as long as something is registered with a click handler.

Basically I checked to make sure the button made is listening to a mouse click event with:

trace(generatedButton.hasEventListener(MouseEvent.CLICK)) which returns true

I proceed to than call this:

generatedButton.dispatchEvent( new MouseEvent(MouseEvent.CLICK, true) );

And nothing happens yet if I physically click the button it works. Is there some security measure that prevents something from being fake clicked unless its origin is strictly from the system mouse?

I even set a timeout call on the click function and moved my cursor over the button and let it fire in case it was an issue of the mouse having to over the object but still nothing. I am kind of stumped at this point.

Any help would be appreciated!

Upvotes: 3

Views: 10231

Answers (4)

ymutlu
ymutlu

Reputation: 6715

You can't simulate click events on a server, it is because security issues. Instead of dispatchingEvent you can call method directly. I ll look for the description but its illegal. You can check firefox firebug logs for error description.

Upvotes: 0

The_asMan
The_asMan

Reputation: 6403

The following code works fine in my local sandbox.

btn.addEventListener( MouseEvent.CLICK, test )

function test( e:Event ):void{
    trace('asdf');
}

btn.dispatchEvent( new MouseEvent( MouseEvent.CLICK ) );

So I can only come to 2 conclusions.
First is a security issue. Flash has some security issues with click events triggering the FileReference class where it can only be used if the stack has a user click in it. This issue may carry over to any artificial dispatching of that event. It works in my sandbox because the restriction doesn't apply here.

The second one is you are dispatching the event to soon and the buttons listener hasn't been added to the button from the api.
In this case try calling the dispatch after the stage render event has been called.

stage.addEventListener( Event.RENDER, test )
function test( e:Event ):void{
    btn.dispatchEvent( new MouseEvent( MouseEvent.CLICK ) );
}

Just my guess anyway.

Upvotes: 2

Ronnie
Ronnie

Reputation: 11198

Ok well I made a quick FLA and wrote this code and the dispatch Mouse event works perfectly..

import flash.events.MouseEvent;

myButton.addEventListener(MouseEvent.CLICK, myClickHandler);

function myClickHandler(e:MouseEvent):void
{
    trace("clicked");
}

setTimeout(function()
{
   myButton.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
},2000);

YouTube api must block against something like this

Upvotes: 2

Ronnie
Ronnie

Reputation: 11198

instead of doing a dispatchEvent, which will eventually call a method anyway, just call the method and since its expecting a MouseEvent, do yourClickMethod(null)

Upvotes: 0

Related Questions