Flash 阻止 JavaScript 事件

发布于 2024-08-25 13:20:09 字数 5046 浏览 7 评论 0原文

这是对原始帖子的编辑,现在我更好地理解了这个问题。 现在有了源代码!

在 IE 中,如果 body(或另一个 html div 具有焦点),那么您可以按键 & 同时点击闪光灯,然后松开...键盘事件永远不会触发。它不会在 javascript 或 flash 中触发。这个 keyup 事件在哪里?

这是事件触发的顺序:

  1. javascriptKeyEvent: bodyDn ** currentFocusedElement: body
  2. javascriptKeyEvent: docDn ** currentFocusedElement: body
  3. actionScriptEvent : activate ** currentFocusedElement: [object]
  4. actionScriptEvent : mouseDown ** currentFocusedElement: [object]
  5. actionScriptEvent : mouseUp ** currentFocusedElement: [object]

后续的 keydown 和 keyup 事件由 flash 捕获,但初始的 keyUp 永远不会在任何地方触发。我需要那个按键!

什么不起作用:

  • 在没有侦听器的情况下检查密钥是否已启动。 检查某个键是否按下?
  • w模式不透明或直接。尽管不透明,重现起来要困难得多。
  • 尽快将焦点推回 javascript 以捕获 up 事件。 (在 javascript 中尝试使用模糊侦听器并在激活 as3 时设置焦点。)
  • 缺少的 keyup
  • jquery 或原型都没有捕获静态和动态 swfobject 发布中

这是 html/javascript:

<html>

<head>
    <script type="text/javascript" src="prototype.js"></script>
    <script type="text/javascript" src="swfobject.js"></script> 

    <script>


    function ic( evt )
    {   Event.observe( $("f1"), 'keyup', onKeyHandler.bindAsEventListener( this, "f1Up" ) );
        Event.observe( $("f2"), 'keyup', onKeyHandler.bindAsEventListener( this, "f2Up" ) );
        Event.observe( document, 'keyup', onKeyHandler.bindAsEventListener( this, "docUp" ) );
        Event.observe( $("body"), 'keyup', onKeyHandler.bindAsEventListener( this, "bodyUp" ) );
        Event.observe( window, 'keyup', onKeyHandler.bindAsEventListener( this, "windowUp" ) );

        Event.observe( $("f1"), 'keydown', onKeyHandler.bindAsEventListener( this, "f1Dn" ) );
        Event.observe( $("f2"), 'keydown', onKeyHandler.bindAsEventListener( this, "f2Dn" ) );
        Event.observe( document, 'keydown', onKeyHandler.bindAsEventListener( this, "docDn" ) );
        Event.observe( $("body"), 'keydown', onKeyHandler.bindAsEventListener( this, "bodyDn" ) );
        Event.observe( window, 'keydown', onKeyHandler.bindAsEventListener( this, "windowDn" ) );

        Event.observe( "clr", "mousedown", clearHandler.bindAsEventListener( this ) );

        swfobject.embedSWF( "tmp.swf",
                            "f2",
                            "100%",
                            "20px",
                            "9.0.0.0", null, {}, {}, {} );
    }

    function clearHandler( evt )
    {   clear( );
    }

    function clear( )
    {   $("log").innerHTML = "";    
    }

    function onKeyHandler( evt, dn )
    {   logIt( "javascriptKeyEvent:"+dn );
    }

    function AS2JS( wha )
    {   logIt( "actionScriptEvent::" + wha );
    }

    function logIt( k )
    {   
        var id = document.activeElement;
        if (id.identify)
        {   id = id.identify();
        }

        $("log").innerHTML = k + " ** focuedElement: " + id + "<br>" + $("log").innerHTML;
    }

    Event.observe( window, 'load', ic.bindAsEventListener(this) );

    </script>

</head>

<body id="body">
<div id="f1"><div id="f2" style="width:100%;height:20px; position:absolute; bottom:0px;"></div></div>

<div id="clr" style="color:blue;">clear</div>

<div id="log" style="overflow:auto;height:200px;width:500px;"></div>
</body>
</html>

这是 as3 代码:

package
{

import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.external.ExternalInterface;

public class tmpa extends Sprite
{

public function tmpa( ):void
{
    extInt("flashInit");
    stage.align = StageAlign.TOP_LEFT;
    stage.scaleMode = StageScaleMode.NO_SCALE;
    stage.addEventListener( KeyboardEvent.KEY_DOWN, keyDnCb, false, 0, true );
    stage.addEventListener( KeyboardEvent.KEY_UP, keyUpCb, false, 0, true );

    stage.addEventListener( MouseEvent.MOUSE_DOWN, mDownCb, false, 0, true );
    stage.addEventListener( MouseEvent.MOUSE_UP, mUpCb, false, 0, true );
    addEventListener( Event.ACTIVATE, activateCb, false, 0, true );
    addEventListener( Event.DEACTIVATE, dectivateCb, false, 0, true );
}

private function activateCb( evt:Event ):void
{   extInt("activate");
}

private function dectivateCb( evt:Event ):void
{   extInt("deactivate");
}

private function mDownCb( evt:MouseEvent ):void
{   extInt("mouseDown");
}

private function mUpCb( evt:MouseEvent ):void
{   extInt("mouseUp");
}

private function keyDnCb( evt:KeyboardEvent ):void
{   extInt( "keyDn" );
}

private function keyUpCb( evt:KeyboardEvent ):void
{   extInt( "keyUp" );
}

private function extInt( wha:String ):void
{   try
    {   ExternalInterface.call( "AS2JS", wha );
    }
    catch (ex:Error)
    {   trace('ex: ' + ex);
    }
}

}
}

this is an edit of the original post now that I better understand the problem. now with source code!

In IE, if body (or another html div has focus), then you keypress & click on flash at the same time, then release... a keyup event is never fired. It is not fired in javascript or in flash. Where is this keyup event?

This is the order of event firing you get instead:

  1. javascriptKeyEvent: bodyDn ** currentFocusedElement: body
  2. javascriptKeyEvent: docDn ** currentFocusedElement: body
  3. actionScriptEvent : activate ** currentFocusedElement: [object]
  4. actionScriptEvent : mouseDown ** currentFocusedElement: [object]
  5. actionScriptEvent : mouseUp ** currentFocusedElement: [object]

Subsequent keydown and keyup events are captured by flash, but that initial keyUp is never fired.. anywhere. And I need that keyup!

What does not work:

  • checking if the key is up without a listener. Check if a key is down?
  • wmode opaque or direct. Although, with opaque, it is far more difficult to reproduce.
  • pushing the focus back to javascript asap to catch the up event. (tried this with a blur listener in javascript and setting focus when as3 is activated.)
  • neither jquery or prototype capture the missing keyup
  • both static and dynamic swfobject publishing

Here is the html/javascript:

<html>

<head>
    <script type="text/javascript" src="prototype.js"></script>
    <script type="text/javascript" src="swfobject.js"></script> 

    <script>


    function ic( evt )
    {   Event.observe( $("f1"), 'keyup', onKeyHandler.bindAsEventListener( this, "f1Up" ) );
        Event.observe( $("f2"), 'keyup', onKeyHandler.bindAsEventListener( this, "f2Up" ) );
        Event.observe( document, 'keyup', onKeyHandler.bindAsEventListener( this, "docUp" ) );
        Event.observe( $("body"), 'keyup', onKeyHandler.bindAsEventListener( this, "bodyUp" ) );
        Event.observe( window, 'keyup', onKeyHandler.bindAsEventListener( this, "windowUp" ) );

        Event.observe( $("f1"), 'keydown', onKeyHandler.bindAsEventListener( this, "f1Dn" ) );
        Event.observe( $("f2"), 'keydown', onKeyHandler.bindAsEventListener( this, "f2Dn" ) );
        Event.observe( document, 'keydown', onKeyHandler.bindAsEventListener( this, "docDn" ) );
        Event.observe( $("body"), 'keydown', onKeyHandler.bindAsEventListener( this, "bodyDn" ) );
        Event.observe( window, 'keydown', onKeyHandler.bindAsEventListener( this, "windowDn" ) );

        Event.observe( "clr", "mousedown", clearHandler.bindAsEventListener( this ) );

        swfobject.embedSWF( "tmp.swf",
                            "f2",
                            "100%",
                            "20px",
                            "9.0.0.0", null, {}, {}, {} );
    }

    function clearHandler( evt )
    {   clear( );
    }

    function clear( )
    {   $("log").innerHTML = "";    
    }

    function onKeyHandler( evt, dn )
    {   logIt( "javascriptKeyEvent:"+dn );
    }

    function AS2JS( wha )
    {   logIt( "actionScriptEvent::" + wha );
    }

    function logIt( k )
    {   
        var id = document.activeElement;
        if (id.identify)
        {   id = id.identify();
        }

        $("log").innerHTML = k + " ** focuedElement: " + id + "<br>" + $("log").innerHTML;
    }

    Event.observe( window, 'load', ic.bindAsEventListener(this) );

    </script>

</head>

<body id="body">
<div id="f1"><div id="f2" style="width:100%;height:20px; position:absolute; bottom:0px;"></div></div>

<div id="clr" style="color:blue;">clear</div>

<div id="log" style="overflow:auto;height:200px;width:500px;"></div>
</body>
</html>

Here is the as3 code:

package
{

import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.external.ExternalInterface;

public class tmpa extends Sprite
{

public function tmpa( ):void
{
    extInt("flashInit");
    stage.align = StageAlign.TOP_LEFT;
    stage.scaleMode = StageScaleMode.NO_SCALE;
    stage.addEventListener( KeyboardEvent.KEY_DOWN, keyDnCb, false, 0, true );
    stage.addEventListener( KeyboardEvent.KEY_UP, keyUpCb, false, 0, true );

    stage.addEventListener( MouseEvent.MOUSE_DOWN, mDownCb, false, 0, true );
    stage.addEventListener( MouseEvent.MOUSE_UP, mUpCb, false, 0, true );
    addEventListener( Event.ACTIVATE, activateCb, false, 0, true );
    addEventListener( Event.DEACTIVATE, dectivateCb, false, 0, true );
}

private function activateCb( evt:Event ):void
{   extInt("activate");
}

private function dectivateCb( evt:Event ):void
{   extInt("deactivate");
}

private function mDownCb( evt:MouseEvent ):void
{   extInt("mouseDown");
}

private function mUpCb( evt:MouseEvent ):void
{   extInt("mouseUp");
}

private function keyDnCb( evt:KeyboardEvent ):void
{   extInt( "keyDn" );
}

private function keyUpCb( evt:KeyboardEvent ):void
{   extInt( "keyUp" );
}

private function extInt( wha:String ):void
{   try
    {   ExternalInterface.call( "AS2JS", wha );
    }
    catch (ex:Error)
    {   trace('ex: ' + ex);
    }
}

}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

二手情话 2024-09-01 13:20:10

这是我期望发生的情况:当焦点位于 html 中时按下按键。您单击闪光灯对象,焦点就会转到该对象。当按键被抬起时,由于 html 不再具有焦点,因此它不知道按键被抬起。您可以通过仅执行相同的操作来轻松测试这一点,而不是单击 Flash 对象,单击另一个窗口并尝试相同的操作,因为这实际上就是正在发生的事情。

至于在 Flash 中没有发生按键向上事件,这可能是因为它不会在没有按下按键的情况下生成按键向上事件,而它从未得到过按键事件,因为焦点仍在 html 中。

我可以想到两种可能的解决方案。首先是确保 Flash 对象以焦点开头。第二种方法是在 Flash 对象上覆盖一个空 div,这样它就永远不会接收焦点。

Here is what I expect is going on: You keypress while focus is in html. You click the flash object and focus goes to that. When the key is lifted, because the html no longer has focus, it doesn't know about the key up. You could easily test this by doing the same thing only instead of clicking on the flash object, click on another window and try the same thing, because that is effectively what is happening.

As for the key up event not happening in flash, that is likely because it doesn't generate key up events without getting a key down, which it never got because focus was still in html.

I can think of two possible solutions to this. The first is to make sure that the flash object starts with focus. The second is to overlay an empty div over the flash object so it never receives focus.

人生百味 2024-09-01 13:20:10

将您的 SWF 嵌入放在任何其他 JavaScript 之前。

swfobject.embedSWF( "tmp.swf",
                            "f2",
                            "100%",
                            "20px",
                            "9.0.0.0", null, {}, {}, {} );

我会在所有函数之外或在文档就绪的 jQuery 函数中尝试它,因为 Flash 获得无穷大的 z 索引,并且应该在其他任何事情之前接受击键,除非在第一次按键发生时它尚未存在。

我的猜测是,第一个密钥发生在任何东西能够接收它之前。在文档就绪函数中尝试使用 jQuery 触发 keyup 事件。

Place your SWF Embed before any other JavaScript.

swfobject.embedSWF( "tmp.swf",
                            "f2",
                            "100%",
                            "20px",
                            "9.0.0.0", null, {}, {}, {} );

I would try it outside of all functions or in a document ready jQuery function because Flash gets a z-index of infinity and should accept key strokes before anything else unless it is not yet present when the first keyup occurs.

My guess is that the first key up is occurring before anything is able to receive it. Try your keyup event trigger with jQuery in an on document ready function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文