Here's another example using jQuery. You can see some sort of demo here. It traces the keypresses from the browser to a textbox.
Your JavaScript would be
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 would 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'll need to import jQuery in your html file and that's about it. jQuery is cross-browser, so no problems should arise. Tested on Safari, Firefox and Opera (OSX).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…