Here is an example of using SWFObject / javascript / AS3 + ExternalInterface It can use some adaptation for cross-browser operation. I tried this only on FF3 (OSX).
First, a document class containing a simple log field (for traces).
It simply defines an ExternalInterface callback that listens for a method call called flashLog , which will be handled by the private setMessage (... params) method
package { import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.external.ExternalInterface; import flash.text.TextField; public class KeyStroke extends Sprite { private var tf:TextField; public function KeyStroke() { stage.scaleMode = StageScaleMode.NO_SCALE; stage.align = StageAlign.TOP_LEFT; tf = addChild(new TextField()) as TextField; tf.autoSize = 'left'; if(ExternalInterface.available) { if(ExternalInterface.addCallback("flashLog", setMessage)) { tf.text = "addCallback() failed :("; } else { tf.text = "flash waiting"; } } else { setMessage("ExternalInterface not available!"); } } private function setMessage(...params):void { tf.text = "message : " + params.toString(); } } }
Paste the exported SWF through SWFObject by adding the allowScriptAccess attribute, you will also need to specify an id so that we can find the SWF further (in this case myMovie ):
var so = new SWFObject('KeyStroke.swf', 'myMovie', '800', '100', '9', '#f0f0f0'); so.addParam("allowScriptAccess","always"); so.write('content');
Create a javascript function to handle keystrokes:
<script type="text/javascript"> function keyPressHandler(e) { </script>
Register keyPressHandler somehow (there are better ways with a prototype, etc.):
<body onKeyPress="keyPressHandler(event);" >
It should be.
source share