Javascript for actionscript adventure transfer utility?

Is there an existing javascript library for relaying keypress events in the browser (or some divs) to flash memory? I hope there can be a library view like that for events in mousewheel ?

Something like this does a great job with javascript keyboard shortcuts. I suppose I could just listen to these events and transfer those that I need in the flash?


EDIT: These are great examples, however, if the focus is focus, then the javascript keystrokes are lost. How can you ensure that all key events go through javascript?

+4
source share
4 answers

Here is another example of using jQuery. You can see the demo here . It tracks keystrokes from the browser to the text box.

Your javascript will

var altPressed = false; var ctrlPressed = false; function getFlashMovie(movieName) { var isIE = navigator.appName.indexOf("Microsoft") != -1; return (isIE) ? window[movieName] : document[movieName]; } function sendCode(code) { movie = getFlashMovie('keyboard-listener'); movie.keyEvent(code); } function activeKey(e) { e.preventDefault(); if (e.which == 18) altPressed = true; if (e.which == 17) ctrlPressed = true; if ((e.which != 18)&&(e.which != 17)) sendCode((altPressed?'alt+':'')+(ctrlPressed?'ctrl+':'')+String.fromCharCode(e.which)); } function inactiveKey(e) { if (e.which == 18) altPressed = false; if (e.which == 17) ctrlPressed = false; } $(document).ready(function() { $(document).keydown(activeKey); $(document).keyup(inactiveKey); }); 

Inside the Flash movie, you will have the following code:

 ExternalInterface.addCallback('keyEvent',keyEvent); function keyEvent(code:String):void { // do something with the "code" parameter, that looks like "alt+ctrl+D", may use .split('+'), etc } 

You will need to import jQuery into your html file and about that. jQuery is a cross browser, so there are no problems. Tested on Safari, Firefox and Opera (OSX).

+2
source

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) { // Calls the registered callback within the flash movie getMovie('myMovie').flashLog("Key Down!"+e.charCode) } function getMovie(movieName) { return document.getElementById(movieName); } </script> 

Register keyPressHandler somehow (there are better ways with a prototype, etc.):

 <body onKeyPress="keyPressHandler(event);" > 

It should be.

+1
source

Replace ctrl with the alt / option key, but it does not handle the option on mac:

This works: // WORKS ONLY IN THE BROWSER: // Tested on SAFARI 4.0 Mac and Firefox 3.5 Mac

 function onKey_Up(e:KeyboardEvent):void { tf2.appendText("\n"+e.keyCode) //alt+C if(e.keyCode==231){ tf2.appendText("\nALT + C") } //alt+V if(e.keyCode==175){ tf2.appendText("\nALT + V") } //alt+X if(e.keyCode==188){ tf2.appendText("\nALT + X") } } 
0
source

When the SWF has focus, you can just listen in actionscript for keystrokes. And if you want, you can send them also in Javascript.

 addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown); private function onKeyDown(e:KeyboardEvent):void { // handle key down, } 
0
source

All Articles