flash blocking javascript events

Posted by jedierikb on Stack Overflow See other posts from Stack Overflow or by jedierikb
Published on 2010-03-16T19:38:31Z Indexed on 2010/03/17 1:01 UTC
Read the original article Hit count: 763

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 ** currentFocuedElement: body
  2. javascriptKeyEvent:docDn ** currentFocuedElement: body
  3. actionScriptEvent::activate ** currentFocuedElement: [object]
  4. actionScriptEvent::mouseDown ** currentFocuedElement: [object]
  5. actionScriptEvent::mouseUp ** currentFocuedElement: [object]

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

Here is the html/javascript:

<html>

<head>
    <script type="text/javascript" src="p.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);
    }
}

}
}

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about javascript-events