A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: triggering mouse events w/o the mouse

  1. #1
    absolutezero3424
    Join Date
    Nov 2006
    Posts
    508

    triggering mouse events w/o the mouse

    I have a button that triggers some code and I want to trigger that same code for initialization purposes w/o the user having to click the button....how would I programmatically trigger that buttons event?

    thx for the help in adv.!

  2. #2
    trace("AKA: Biro Barna");
    Join Date
    Oct 2007
    Location
    RO.Timişoara
    Posts
    1,403
    Is your code triggered in the buttons call function or is it a separate function ?

    Here's a fast "example".

    yourButtonsInstanceName.addEventListener(MouseEven t.CLICK, onClick);

    function onClick(event:MouseEvent):void
    {
    // do something
    }

    and for your second button you should only do a:

    secondButton.addEventListener(MouseEvent.CLICK, onClick);

    If you are calling a certain function inside onClick at your first button then you could do something like this for your second button:


    secondButton.addEventListener(MouseEvent.CLICK, onSecondClick);

    function onSecondClick(event:MouseEvent):void
    {
    yourFunction(); // call the specific function
    }

    Hope that helped, if not, try uploading a file or something so that I ( we ) can understand much better what your problem is.

    Good luck.



    | Windows MSN: birobarna [at] hotmail [dot] com | Skype: barna.biro |
    WebLog: http://blog.wisebisoft.com/ |
    | Software Developer / Flash & Flex Developer | Student ( Computer Science ) | Interested in: Sharing Knowledge |
    |
    Romanian Adobe Flash, Flex, AIR Forum: http://www.flashforum.ro/
    | By perseverance the snail reached the ark. |


  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Your question was vague, but I can guess that you mean one of two things. First: "How can I make the function not depend on the event?". That's fairly easy. You can make the event argument to the function have a default value.
    Code:
    public function handleClick(event:MouseEvent = null){
     //stuff that never uses event
    }
    Now handleClick can be used as a MouseEvent listener AND it can be called with no arguments.

    Second: "How can I simulate mouse actions without actually relying on the user?"

    This one is a little more complex, but also do-able. You can use VirtualMouse, which is a handy little class from Senocular which simulates the mouse and can dispatch actual mouse events. Check the com.senocular.ui package under ActionScript 3 here: http://senocular.com/flash/actionscript.php

  4. #4
    absolutezero3424
    Join Date
    Nov 2006
    Posts
    508
    Quote Originally Posted by 5TonsOfFlax
    You can make the event argument to the function have a default value.
    Code:
    public function handleClick(event:MouseEvent = null){
     //stuff that never uses event
    }
    yes, that was a good suggestion and one that I will probably use...the other way I was thinking of involved using dispatchEvent, but I'm not familiar with using it and I'm sure that's what senocular utilizes in those classes probably.
    thanks guys!

  5. #5
    Junior Member
    Join Date
    Jan 2009
    Posts
    1
    You could make your button dispatch a click event this way:
    Code:
    yourButtonsInstanceName.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
    This triggers the event listener as if the user had clicked the button.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center