javascript 到 actionscript 按键传递实用程序? [英] javascript to actionscript keypress passing utility?

查看:25
本文介绍了javascript 到 actionscript 按键传递实用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有现有的 javascript 库可以将浏览器(或某些 div)中的按键事件中继到 flash 中?我希望可能有一种像这样的用于鼠标滚轮事件的图书馆?>

this 之类的东西可以很好地处理 JavaScript 键盘快捷键.我想我可以只监听这些事件并将我想要的事件传递到 Flash 中?

<小时>

这些都是很好的例子,但是,如果 Flash 具有焦点,那么 javascript 按键就会丢失.如何确保所有关键事件都通过 javascript?

解决方案

这是另一个使用 jQuery 的示例.您可以在此处看到某种演示.它跟踪从浏览器到文本框的按键操作.

你的 JavaScript 将是

var altPressed = false;var ctrlPressed = false;函数 getFlashMovie(movieName){var isIE = navigator.appName.indexOf("Microsoft") != -1;返回(isIE)?窗口[电影名]:文档[电影名];}函数发送代码(代码){电影 = getFlashMovie('键盘监听');电影.keyEvent(代码);}函数 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));}函数 inactiveKey(e) {if (e.which == 18) altPressed = false;if (e.which == 17) ctrlPressed = false;}$(document).ready(function() {$(document).keydown(activeKey);$(document).keyup(inactiveKey);});

在 Flash 影片中,您将拥有以下代码:

ExternalInterface.addCallback('keyEvent',keyEvent);功能键事件(代码:字符串):无效{//用code"参数做一些事情,看起来像alt+ctrl+D",可以使用.split('+')等}

您需要在您的 html 文件中导入 jQuery,仅此而已.jQuery 是跨浏览器的,所以应该不会出现问题.在 Safari、Firefox 和 Opera (OSX) 上测试.

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?

解决方案

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

这篇关于javascript 到 actionscript 按键传递实用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆