Here's an example using SWFObject / javascript / AS3 + ExternalInterface
It may use some adaption to work cross-browser. I tried it on FF3(OSX) only.
First a document class containing a simple log field (for traces).
It simply defines an ExternalInterface callback listening to a method call named flashLog that will handled by the private method setMessage(...params)
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();
}
}
}
Embed the exported SWF via SWFObject adding the allowScriptAccess attribute, you will also need to give an id so we can locate the SWF further on (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 the key presses :
<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 the keyPressHandler somehow (there's better ways with prototype etc.) :
<body onKeyPress="keyPressHandler(event);" >
That should be it.