Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 487
Stefan Woskowiak Avatar asked Apr 27 '26 19:04

Stefan Woskowiak


2 Answers

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

like image 118
Ronnie Avatar answered Apr 30 '26 15:04

Ronnie


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.

like image 45
The_asMan Avatar answered Apr 30 '26 14:04

The_asMan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!