Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
621 views
in Technique[技术] by (71.8m points)

flash - javascript to actionscript keypress passing utility?

Is there an existing javascript library for relaying key press events in the browser (or certain divs) into flash? I am hoping there might be a library kind of like this one for mousewheel events ?

Something like this handles javascript keyboard shortcuts great. I suppose I could just listen for those events and pass the ones I want into flash?


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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...