A Flash Developer Resource Site

Results 1 to 20 of 20

Thread: JAVASCRIPT running flash button

  1. #1
    Senior Member
    Join Date
    Aug 2009
    Posts
    113

    JAVASCRIPT running flash button

    Hi! I think im almost there. Please help me out. I need to complete a script to trigger a click event using keyboard shortcuts:

    *myListener is the name of my button

    Code:
    <script>
            function searchKeyPress(e)
            {
                    if (e.keyCode == 113)
                    {
                            document.getElementById('myListener').click();
                    }
            }
            </script>

    or

    Code:
    <script language=javascript type="text/javascript" >
    function ClickMe() {document.getElementById("myListener").click();}
    </script>

    How would javascript determine that this Id name, myListener, is inside a flash file?

  2. #2
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    I don't see what your JS function is supposed to do. Also, the element and its ID need to be in the DOM, like a div for instance; certainly not a button instance name inside an embeded swf. A JS function, like AS, can accept parameters, though. So if you need to pass a variable value, insert it in your JS.

    if you HAVE to use javascript, use ExternalInterface.call to call the JS from flash.

    If not, this will suffice in Flash alone. Use a dyn txtfield with instance name keynum. Since you'll need to test the swf in standalone. The Function keys in authoring or test mode being reserved :

    PHP Code:
    var myListener:Object = new Object();
    myListener.onKeyDown = function () {
        if (
    Key.getCode() == 113) {
            
    keynum.text "F2 pressed";
        }
    }
    myListener.onKeyUp = function () {
        
    keynum.text "F2 released";
    }
    Key.addListener(myListener); 
    gparis

  3. #3
    Senior Member
    Join Date
    Aug 2009
    Posts
    113
    this will be inside flash right?

    What I want to do is allow javascript to trigger the flash button. I have a quiz like flash file but the textbox and questions are separated. They are in javascript. Then I have a Clue button in flash which will operate this:

    Code:
    getURL("javascript:command('clue',1)","_self");
    and the Clue button should have a keyboard shortvut F2.

    So if the client is typing in the textbox (focus is in html), he/she wont be able to press F2 and operate the flash button function. He/she needs to click on the swf interface first before he could press F2.

    The problem is not all clients know it. So we have to do something that even if the focus is in html, and the client presses F2, he/she would still be able to operate the Clue button.


  4. #4
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    PHP Code:
    import flash.external.ExternalInterface;
    ExternalInterface.call("focus"); 
    will set the focus to the Flash

    Check the ExternalInterface Class in the liveDocs (link in my footer) for all communications between JS and Actionscript.

    Your first post was unclear to me. My 1st answer was on how to trigger something in Flash when a key is pressed. Obviously not what you need right now.

    gparis

  5. #5
    Senior Member
    Join Date
    Aug 2009
    Posts
    113
    yes, thank you. Is this externalinterface has something to do with AS3? bec i started creating the website in AS2..

  6. #6
    Senior Member
    Join Date
    Aug 2009
    Posts
    113
    anyone who could help me? Bec. I dont really have an idea how to start. Is it possible?


    and can someone tell me what is the meaning of this.addEventListener(KeyboardEvent.KEY_UP,keyPres sed); in this code:

    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init();">
    <mx:Script>
    <![CDATA[
    private function init():void {
    i.setFocus();
    this.addEventListener(KeyboardEvent.KEY_UP,keyPres sed);
    }

    private function keyPressed(event:KeyboardEvent):void {
    if(event.keyCode.toString()=="84" && event.ctrlKey==true)
    ExternalInterface.call('newtab');
    }

    ]]>
    </mx:Script>
    <mx:TextInput x="23" y="268" width="256" id="i" text="Text Box"/>
    </mx:Application>

    <script type="text/javascript">
    function newtab(e){
    document.body.focus();
    window.open('about:blank');
    }
    </script>


    Or is it possible to setfocus on both flash file and html?

    I mean, F1, F2, F3 when clicked will set focus on flash and run Key.getCode() == 113 from flash
    but letters and arrow keys will function in textbox (html)..
    Last edited by ayusuits; 11-02-2009 at 04:43 AM.

  7. #7
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    Or is it possible to setfocus on both flash file and html?

    I mean, F1, F2, F3 when clicked will set focus on flash and run Key.getCode() == 113 from flash
    I gave you both codes. Put the EI code in your onKeyDown.

    gparis

  8. #8
    Senior Member
    Join Date
    Aug 2009
    Posts
    113
    import flash.external.ExternalInterface;
    ExternalInterface.call("focus");

    myListener = new Object();
    myListener.onKeyDown = function() {

    if (Key.getCode() == 113) {
    getURL("javascript:command('listen',2)","_self");
    }
    };
    Key.addListener(myListener);
    is there something wrong with the actionscript? coz if I am typing in html textbox and I press F2, still, the clue button is not functioning.
    Last edited by ayusuits; 11-02-2009 at 09:47 PM.

  9. #9
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    getURL("javascript:command('listen',2)","_self");
    Nope. getURL won't work.


    ExternalInterface.call("listen(2)");

    as long as you have a JS function called listen which takes a number, like 2, as parameter.

    gparis

  10. #10
    Senior Member
    Join Date
    Aug 2009
    Posts
    113
    so I changed it to:

    Code:
    import flash.external.ExternalInterface;
    ExternalInterface.call("listen(2)");
    
    myListener = new Object();
    myListener.onKeyDown = function() {
    
    if (Key.getCode() == 113) {
    getURL("javascript:command('listen',2)","_self");
    }
    };
    Key.addListener(myListener);

    and then javascript will just have to call ExternalInterface.call("listen(2)");
    to run the same script with the flash button?
    Is my understanding correct?

  11. #11
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    no. replace the getURL.

    gparis

  12. #12
    Senior Member
    Join Date
    Aug 2009
    Posts
    113
    but the geturl calls the javascript.. that is the function of the button.

  13. #13
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    That's what ExternalInterface does. Calling JS.

    gparis

  14. #14
    Senior Member
    Join Date
    Aug 2009
    Posts
    113
    ah i got it. it's the other way to call javascript. thanks!

  15. #15
    Senior Member
    Join Date
    Aug 2009
    Posts
    113
    did I miss something?

    PHP Code:
    import flash.external.*;

    var 
    methodName:String "sendTextFromHtml";
    var 
    instance:Object null;
    var 
    method:Function = receiveTextFromHtml;
    var 
    wasSuccessful:Boolean ExternalInterface.addCallback(methodNameinstancemethod)

    function 
    receiveTextFromHtml(key) {
        if (
    key.text == F2) {
        
    ExternalInterface.call("listen(2)","_self");
        }
        else if (
    key.text == Enter) {
        
    ExternalInterface.call("check(1)","_self");
        }
    };

    ExternalInterface.call("receiveTextFromFlash"myListener); 
    The programmer will give a shortcut key to the text Enter, F2, F3, etc..

    myListener is the instance name of the flash button
    Last edited by ayusuits; 11-03-2009 at 09:53 PM.

  16. #16
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    post your javascripts, the functions listen and check, cause i have no idea what you're trying to make happen just by looking at your AS.

    And the _self param is ok with getURL but has no place in a JS call.

    gparis

  17. #17
    Senior Member
    Join Date
    Aug 2009
    Posts
    113
    nevermind. i already got it. thanks gparis
    Attached Files Attached Files
    Last edited by ayusuits; 11-04-2009 at 03:19 AM.

  18. #18
    Senior Member
    Join Date
    Aug 2009
    Posts
    113

    GetURL converting to External Internal

    Hi! Ive this from gparis, but unfortunately, the programmer said, the code is not working/running

    I have this, originally:
    Code:
    getURL("javascript:command('clue',1)","_self");
    gparis taught me this:

    Code:
    ExternalInterface.call("clue(1)");
    So should I change it to:

    ExternalInterface.call("command(clue, 1)");


    and how do I convert this: getURL("javascript:loadHistory(+1)","_self");


    Help me please... thanks!

  19. #19
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    if js function clue has a parameter, and 1 is supposed to be it, the call is different:
    ("clue", "1") use commas to separate the functions and its params

    Main difference in the call syntax would be:
    getURL("javascript:function")
    ExternalInterface.call('function')

    now, i really think that you should read the documentation.

    gparis

  20. #20
    Senior Member
    Join Date
    Aug 2009
    Posts
    113
    I dont know how the programmer was doing it. I tried

    ExternalInterface.call('command', listen);

    it didnt work.



    Ah this one works:

    ExternalInterface.call("command('listen')");

    Thanks~

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