To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here


A Flash Developer Resource Site

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Reply
 
Thread Tools Search this Thread Display Modes
Old 11-01-2009, 10:55 PM   #1
ayusuits
Member
 
Join Date: Aug 2009
Posts: 87
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?
ayusuits is offline   Reply With Quote
Old 11-02-2009, 12:17 AM   #2
gparis
Super Moderator
 
Join Date: Aug 2000
Location: Montréal
Posts: 13,242
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
__________________


AS 2.0 Forum Guidelines || Use PHP tags for code samples || Flash9 LiveDocs || AS 2.0 Language reference (CS4)
gparis is offline   Reply With Quote
Old 11-02-2009, 01:30 AM   #3
ayusuits
Member
 
Join Date: Aug 2009
Posts: 87
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.

ayusuits is offline   Reply With Quote
Old 11-02-2009, 02:02 AM   #4
gparis
Super Moderator
 
Join Date: Aug 2000
Location: Montréal
Posts: 13,242
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
__________________


AS 2.0 Forum Guidelines || Use PHP tags for code samples || Flash9 LiveDocs || AS 2.0 Language reference (CS4)
gparis is offline   Reply With Quote
Old 11-02-2009, 02:05 AM   #5
ayusuits
Member
 
Join Date: Aug 2009
Posts: 87
yes, thank you. Is this externalinterface has something to do with AS3? bec i started creating the website in AS2..
ayusuits is offline   Reply With Quote
Old 11-02-2009, 03:51 AM   #6
ayusuits
Member
 
Join Date: Aug 2009
Posts: 87
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:

Quote:
<?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.
ayusuits is offline   Reply With Quote
Old 11-02-2009, 04:21 PM   #7
gparis
Super Moderator
 
Join Date: Aug 2000
Location: Montréal
Posts: 13,242
Quote:
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
__________________


AS 2.0 Forum Guidelines || Use PHP tags for code samples || Flash9 LiveDocs || AS 2.0 Language reference (CS4)
gparis is offline   Reply With Quote
Old 11-02-2009, 09:19 PM   #8
ayusuits
Member
 
Join Date: Aug 2009
Posts: 87
Quote:
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.
ayusuits is offline   Reply With Quote
Old 11-03-2009, 01:59 AM   #9
gparis
Super Moderator
 
Join Date: Aug 2000
Location: Montréal
Posts: 13,242
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
__________________


AS 2.0 Forum Guidelines || Use PHP tags for code samples || Flash9 LiveDocs || AS 2.0 Language reference (CS4)
gparis is offline   Reply With Quote
Old 11-03-2009, 02:07 AM   #10
ayusuits
Member
 
Join Date: Aug 2009
Posts: 87
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?
ayusuits is offline   Reply With Quote
Old 11-03-2009, 02:44 AM   #11
gparis
Super Moderator
 
Join Date: Aug 2000
Location: Montréal
Posts: 13,242
no. replace the getURL.

gparis
__________________


AS 2.0 Forum Guidelines || Use PHP tags for code samples || Flash9 LiveDocs || AS 2.0 Language reference (CS4)
gparis is offline   Reply With Quote
Old 11-03-2009, 02:47 AM   #12
ayusuits
Member
 
Join Date: Aug 2009
Posts: 87
but the geturl calls the javascript.. that is the function of the button.
ayusuits is offline   Reply With Quote
Old 11-03-2009, 02:42 PM   #13
gparis
Super Moderator
 
Join Date: Aug 2000
Location: Montréal
Posts: 13,242
That's what ExternalInterface does. Calling JS.

gparis
__________________


AS 2.0 Forum Guidelines || Use PHP tags for code samples || Flash9 LiveDocs || AS 2.0 Language reference (CS4)
gparis is offline   Reply With Quote
Old 11-03-2009, 09:04 PM   #14
ayusuits
Member
 
Join Date: Aug 2009
Posts: 87
ah i got it. it's the other way to call javascript. thanks!
ayusuits is offline   Reply With Quote
Old 11-03-2009, 09:38 PM   #15
ayusuits
Member
 
Join Date: Aug 2009
Posts: 87
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(methodName, instance, method)

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.
ayusuits is offline   Reply With Quote
Old 11-03-2009, 11:27 PM   #16
gparis
Super Moderator
 
Join Date: Aug 2000
Location: Montréal
Posts: 13,242
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
__________________


AS 2.0 Forum Guidelines || Use PHP tags for code samples || Flash9 LiveDocs || AS 2.0 Language reference (CS4)
gparis is offline   Reply With Quote
Old 11-03-2009, 11:39 PM   #17
ayusuits
Member
 
Join Date: Aug 2009
Posts: 87
nevermind. i already got it. thanks gparis
Attached Files
File Type: doc write.doc (42.5 KB, 1 views)

Last edited by ayusuits; 11-04-2009 at 03:19 AM.
ayusuits is offline   Reply With Quote
Old 11-12-2009, 10:11 PM   #18
ayusuits
Member
 
Join Date: Aug 2009
Posts: 87
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!
ayusuits is offline   Reply With Quote
Old 11-13-2009, 12:29 AM   #19
gparis
Super Moderator
 
Join Date: Aug 2000
Location: Montréal
Posts: 13,242
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
__________________


AS 2.0 Forum Guidelines || Use PHP tags for code samples || Flash9 LiveDocs || AS 2.0 Language reference (CS4)
gparis is offline   Reply With Quote
Old 11-13-2009, 01:23 AM   #20
ayusuits
Member
 
Join Date: Aug 2009
Posts: 87
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~
ayusuits is offline   Reply With Quote
Reply

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 12:24 AM.


internet.commerce
Be a Commerce Partner
 »  »  »  »  »  »  »
 »  »  »  »  »  »
 

    

Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.