One of the really cool additions to Adobe AIR 1.5.2 was the ability to invoke preventDefault() upon a FullScreenEvent when using StageDisplayState.FULL_SCREEN_INTERACTIVE. I always had a workaround for Windows but my method would always crash the app when run on a Mac. The ability to go full screen and lock that down is essential to kiosk-type applications. This does not prevent a user from closing out the app in some other way, however.
While working on an AIR-based kiosk project this week, I was given the request to prevent COMMAND+Q from closing the application on a Mac. They really wanted this thing locked down to the point where the only way to get out of the app would be to enter some random key combination. Took a little bit of digging around to figure out how to both prevent the default closing action but still allow the app to close in the event that the correct key commands were entered. I share this here as I can see others having this same need.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | <?xml version="1.0" encoding="utf-8"?> <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="400" applicationComplete="init()"> <fx:Declarations> <!-- Place non-visual elements (e.g., services, value objects) here --> </fx:Declarations> <fx:Script> <![CDATA[ import mx.events.CloseEvent; import flash.events.Event; import flash.display.StageDisplayState; import flash.events.KeyboardEvent; import flash.events.FullScreenEvent; private var commandEntered:Boolean; private function init():void { this.stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE; this.addEventListener(Event.CLOSING, appClosing, false, 0, true); stage.addEventListener(KeyboardEvent.KEY_DOWN, killApp, false, 0, true); stage.addEventListener(FullScreenEvent.FULL_SCREEN, displayStateChanged, false, 0, true); } private function displayStateChanged(e:FullScreenEvent):void { e.preventDefault(); } private function killApp(e:KeyboardEvent):void { e.preventDefault(); if(e.keyCode == 70 && e.ctrlKey && e.altKey){ commandEntered = true; this.close(); }else{ commandEntered = false; } } private function appClosing(e:Event):void { if(!commandEntered){ e.preventDefault(); } } ]]> </fx:Script> <s:Label text="CTRL + ALT + f" horizontalCenter="0" verticalCenter="0" fontWeight="bold" fontSize="32"/> </s:WindowedApplication> |
UPDATE: If you have a stateful application, you may want to consider the following information, as it may be disruptive in handling these events properly: http://beingwicked.com/development/flex-initialize-and-the-hassles-of-changing-state/
