views:

187

answers:

1

There is an event which is dispatched when the context menu (right click menu) in actionscript for flash has been opened:

ContextMenuEvent.MENU_SELECT

Now, is there an event which is dispatched when the menu has been closed?

+1  A: 

Good question. That would make a nice feature request, an ContextMenuEvent.MENU_CLOSED event :)

I think I have half you're answer. Here's my idea:

var myContextMenu:ContextMenu = new ContextMenu();
var menuLabel:String = "Custom Item";
var rightClicking:Boolean;

addCustomMenuItems();
myContextMenu.addEventListener(ContextMenuEvent.MENU_SELECT, menuSelectHandler);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseUpHandler);
    var redRectangle = makeRedRectangle();
    redRectangle.contextMenu = myContextMenu;


function makeRedRectangle():Sprite{
    redRectangle = new Sprite();
    redRectangle.graphics.beginFill(0x990000,.2);
    redRectangle.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
    redRectangle.mouseChildren = false;
    addChild(redRectangle);
    return redRectangle;
}

function addCustomMenuItems():void {
    myContextMenu.hideBuiltInItems();
     var item:ContextMenuItem = new ContextMenuItem(menuLabel);
     myContextMenu.customItems.push(item);
     item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, menuItemSelectHandler);
}

function menuSelectHandler(event:ContextMenuEvent):void {
     trace("menuSelectHandler: " + event);
     rightClicking = true;
}

function menuItemSelectHandler(event:ContextMenuEvent):void {
     trace("menuItemSelectHandler: " + event);
}

function mouseUpHandler(event:MouseEvent):void{
    if(rightClicking){
     trace('ContextMenu Closed\nThank You! Come Again!');
     rightClicking = false;
    }
}

Basically I create a sprite that sits on top of everything, but has mouseChildren set to false, so clips bellow it can get the clicks. You might want to have this one transparent. I used this so you get an event fired when you right click over it. When that happens I set rightClicking to true, meaning, I know the right click was pressed, I'm just waiting for something else to happen. There are two options:

  1. The user selects an item from the menu.
  2. The user click away to make the menu go away.

For option 1, if the user selects any of your custom items, that's cool, you can handle that, if not, at least you know what might happen. For option 2 I've setup the listener for the MOUSE_DOWN event, so if the rightClicking was turned on and you got to the mouse down, that's your menu closing.

Hope this helps!

I know, it looks like hacky old school as2, and the code is modified from the documentation example, but it's a thought :)

George Profenza