compilationbrowseropenlaszlolzx

Enabling the F5 function key to reload or recompile OpenLaszlo SWF applications


One of the annoyances of the Flash Player is that the player - once it has the focus - blocks certain key events from being passed on to the browser. Especially for the F5 function key used to reload pages or to recompile the application. As a result you have to click outside the area of the SWF movie for the browser to regain focus before pressing F5.

How can the F5 key still be used with SWF runtime applications to recompile and reload an SWF runtime application?


Solution

  • For all ActionScript 3 based runtimes (SWF10, SWF11, ...) the following code will enable reload of the OpenLaszlo app by pressing F5.

    <canvas>
    
        <passthrough when="$as3">
           import flash.events.Event;
        </passthrough>
    
        <handler name="oninit">
          if ($as3) {
            var sprite = this.getDisplayObject();
            sprite.addEventListener(Event.ACTIVATE, onactivate)
            sprite.addEventListener(Event.DEACTIVATE, ondeactivate);
          }
        </handler>
    
        <method name="onactivate" args="e=null">
          Debug.info('OpenLaszlo app is active, press F5 to reload');
        </method>
    
        <method name="ondeactivate" args="e=null">
          Debug.info('OpenLaszlo SWF is inactive');
        </method>
    
        <switch><when property="$debug">
            <!-- Reload SWF app when F5 is pressed -->
            <command key="['f5']"
                     onselect="lz.Browser.callJS('document.location.reload(true)')"
                     active="$once{$as3}" />
        </when></switch>
    
    </canvas>
    

    First, I'm tracking the Event.ACTIVATE and Event.DEACTIVATE events for the Flash movie. The Event.ACTIVATE event will be sent when the Flash movie receives the focus. To enable reload by pressing F5, a <command /> tag is added - but only when $debug is enabled for the application.

    <switch><when property="$debug">
        <!-- Reload SWF app when F5 is pressed -->
        <command key="['f5']"
                 onselect="lz.Browser.callJS('document.location.reload(true)')"
                 active="$once{$as3}" />
    </when></switch>
    

    The onselect handler triggers reloading the current page through use of the lz.Browser object. The key command is only active for the ActionScript 3 runtime. Now, when you are testing or developing your application in SWF runtime, just press F5 to reload as you would do in the DHTML runtime.