Right Click Menu Tutorial – Part I

There are so many new features in Flash MX 2004 that I haven’t even gotten around to looking at some of them. I knew there was a way to play with the “right-click menu” in there, but just recently started digging into it. Since I haven’t seen much about it, I figured I’d write up what I found, so people will realize how easy it is to customize this element.

  1. The first thing you need to know is that Buttons, TextFields and MovieClips (including components, _root, and _levels) now have a new property called, “menu”. This is an object that represents that right-click menu.
  2. Rather than directly messing with this object, you need to make a new instance of a ContextMenu object, and assign that to the menu property of the element you want to affect. This looks something like this:


    var myMenu:ContextMenu;
    myMenu = new ContextMenu();
    _root.menu = myMenu;

  3. Now you can start messing with the menu. The main thing that anyone wants to do is to hide the built in items that are usually seen on that menu. This is done like so:


    var myMenu:ContextMenu;
    myMenu = new ContextMenu();
    _root.menu = myMenu;
    myMenu.hideBuiltInItems();

    Note that you can’t get rid of the “Settings” and “About” items. Those are there to stay… at least until someone finds a way to hack them out.

  4. Rather than remove ALL of the built in items, you might want to just remove one or a few of them. All of the possible items that can appear on the menu are members of the “builtInItems” object which is a member of the ContextMenu object. You can just set each one to true or false to show or hide it. For instance if we wanted to hide the “Quality” menu item, we could say instead:


    myMenu.builtInItems.quality = false;

    If you’ve set up the code as above, you should get a list of code hints showing the available options right after you type “myMenu.”.

  5. Next, we can set up a function that will be run whenever the right-click menu is opened. We just assign the function to the “onSelect” property of the ContextMenu object:


    myMenu.onSelect = notify;
    function notify() {
    &nbsp&nbsp&nbsp&nbsptrace(“You Right-Clicked!”);
    }

    Of course, you’d probably want to set up something more useful, but you get the idea.

Well, that should be enough to spark your interest and get you playing with it. Part II will cover how to put your own items on the list and react when the user chooses them.

This entry was posted in Flash. Bookmark the permalink.

3 Responses to Right Click Menu Tutorial – Part I

  1. robogrobo says:

    Try this:
    Make a textfield and name it “output”.

    var myMenu:ContextMenu;
    myMenu = new ContextMenu();
    myMenu.hideBuiltInItems();
    myMenu.builtInItems.quality = false;
    function notify() {
    output.text = “right click”;
    }
    myMenu.onSelect = notify;

    _root.menu = myMenu;

  2. kenisalen says:

    Oh,this article is very good,but 🙁 555555555.
    i can’t know this meaning.oh i hate my brain!can you give me a resouse sample!thanx

Comments are closed.