PDA

View Full Version : Prevent closing air app with cmd+w shortkey


ABM
04-08-2008, 02:34 PM
hi!
i was wondering if there is any possibility to prevent closing the air app if the user click on command+w in his keyboard (Mac).

Regards,
Adil

ABM
04-10-2008, 12:24 PM
Hi All,

I have the solution, and it works now for mac(cmd+W or cmd+Q) and win(alt+F4).

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">

<mx:Script>
<![CDATA[
import mx.containers.Canvas;

private var key_down: Boolean;
private var os: String;

private function init(): void
{
os = (Capabilities.os.search("Mac") >= 0) ? 'Mac' : 'Win';
addEventListener( Event.CLOSING, onClosing );
addEventListener( KeyboardEvent.KEY_DOWN, onKeyDown );
addEventListener( KeyboardEvent.KEY_UP, onKeyUp );
}

private function onKeyDown( event: KeyboardEvent ): void
{
key_down = (os == 'Mac') ? event.commandKey : event.altKey;
}

private function onKeyUp( event: KeyboardEvent ): void
{
key_down = false;
}

private function onClosing( event: Event ): void
{
if( key_down && ( navigator.numChildren > 0 ) )
{
navigator.removeChild( navigator.selectedChild );
event.preventDefault();
}
}

private function addNewTab(): void
{
var canvas: Canvas = new Canvas();
canvas.percentWidth = 100;
canvas.percentHeight = 100;
canvas.label = 'Tab ' + ( navigator.numChildren + 1 );
canvas.setStyle( 'backgroundColor', uint( Math.random() * 255 ) << 16 | uint( Math.random() * 255 ) << 8 | uint( Math.random() * 255 ) );

navigator.addChild( canvas );
navigator.selectedIndex = ( navigator.numChildren - 1 );
}
]]>
</mx:Script>
<mx:VBox bottom="10" top="10" left="10" right="10">
<mx:HBox width="100%">
<mx:Button label="Add New Tab" click="addNewTab()"/>
</mx:HBox>
<mx:TabNavigator id="navigator" width="100%" height="100%">
<mx:Canvas label="Tab 1" width="100%" height="100%">
</mx:Canvas>
</mx:TabNavigator>
</mx:VBox>

</mx:WindowedApplication>

Best Regards,
Adil

beantickler
04-11-2008, 07:09 AM
The only bit of relevence is:...

addEventListener( Event.CLOSING, onClosing );
private function onClosing( event: Event ): void
{
event.preventDefault();
}

ABM
04-11-2008, 07:18 AM
Hi, yes is true,

But this small app colse the tabs of the TabNavigator in thesame way as firefox does...

Regards,
Adil