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);
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.
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.
<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)..
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?
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"); } };
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.