JavaScript来动作键preSS传递工具?动作、工具、JavaScript、preSS

2023-09-09 21:21:50 作者:心稳路正精彩

有没有在浏览器(或特定的div)到闪存中继键preSS事件现有的JavaScript库?我希望有可能是一个图书馆那种像这样的鼠标滚轮事件?

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 ?

喜欢的东西这处理JavaScript的键盘快捷键巨大。我想我可以只听这些事件和那些我想要传递到闪存?

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?

编辑:这是一个很好的例子,但是,如果闪光灯的焦点,那么JavaScript的按键输入。你怎么能确保所有的重要事件经过的JavaScript?

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?

推荐答案

下面是使用jQuery的另一个例子。你可以看到某种形式的演示这里。它跟踪从浏览器的关键presses到一个文本框。

Here's another example using jQuery. You can see some sort of demo here. It traces the keypresses from the browser to a textbox.

您的JavaScript将是

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);
    });

在Flash影片,您将有以下code:

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
}

您需要导入 jQuery的在你的HTML文件,仅此而已。 jQuery是跨浏览器的,因此不会出现问题。测试在Safari浏览器,Firefox和Opera(OSX)。

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