Monday, January 7, 2008

Make your own custom right-click menu with AS3

Update: Just a note that the "Settings" and "About Adobe Flash Player..." buttons are a requirement of the player. It is not possible to remove them via AS.

I am going to show you how to customize the right-click menu in ActionScript 3 as I have done in my photo gallery. AS3 already provides you with a class that controls the menu: ContextMenu, and classes that handle the properties and events: ContextMenuItem and ContextMenuEvent. It gives you everything you need and nothing you don't. It's pretty straight-forward. So here's a step-by-step (Note: if you're working in Flex, you will need to import the classes)

1. Create an instance of ContextMenu in order to use it:
var myMenu:ContextMenu = new ContextMenu();

2. This is pretty self-explanatory. Hide the default items:
myMenu.hideBuiltInItems();

3a. Create your custom item as a ContextMenuItem:
var menuItem1:ContextMenuItem = new ContextMenuItem("Hello World");

3b. If you want to add action to your custom item, add the item and then add a listener to listen for the event:
var menuItem2:ContextMenuItem = new ContextMenuItem("Go to My Page");
menuItem2.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, doSomething);


4. Add your custom item to "myMenu":
myMenu.customItems.push(menuItem1);

5. Tell Flash where your custom menu appears when you right-click. Generally speaking you would want it to be anywhere and everywhere you right-click. But if you want your custom menu to appear only over a button or movie clip, you would just change "this" to the button or movie clip.
this.contextMenu = myMenu;

6. Finally, define your listener function for the menu item that contains the action. In this example, it takes you to my site:
function doSomething(e:ContextMenuEvent):void {
var url:String = "http://www.kennylin.com";
var request:URLRequest = new URLRequest(url);
navigateToURL(request, '_blank');
}


That's it. Export and you should see the same result as below:



Oh and of course, right-click here for the source.

16 comments:

phoechan said...

Hi
I want to hidden right click menu or (Setting , About Adobe Flash Player) . Is it possible.
Help me please.My Email Address is phoecxan@gmail.com and reply me.
Thank U Very Much .

Chris Kitching said...

I'm afraid to say it's not possible to completly remove the menu, as the blog says, the best you can do is hide some of the built in items, but "Settings" and "About Adobe Flash Player" will never go away, whatever you do.

Flash Blog said...

I find this important with SWF files that need the forward movie option disabled for security purposes, especially swf files based on password/login scripts.

Nice post!

Tom said...

Theres an error in step 4 of your code.It says:

myMenu.customItems.push(menuItem1);

When it should be:

myMenu.customItems.push(menuItem1, menuItem2);

Which means it shows your example below correctly.

Moyea Web Player said...

Thanks a lot. But it seems can not remove "About Adobe Flash Player".....

sadik said...

Thanks it helped a lot

Dubai Web Design, Development said...

Thanks for nice script. I will use this script in my digital presentation for companies.

Jay said...

All very helpful... thanks for the time-saving tips...

Anonymous said...

thanks :-)

Anonymous said...

Very nice! I was looking for an AS3 way to do this!
And yes you can't remove the Settings and About options..

Wanja Chresta said...

Flash Blog said...
I find this important with SWF files that need the forward movie option disabled for security purposes, especially swf files based on password/login scripts.


This is a stupid thing to do as it is not a guarantee for safety. The user can always load the swf into the external player where he doesn't need the context menu to use the forward option. Never rely on obscurity when it comes to security - take your time and do it right, so that even a forward command wont do any harm.

Also remember that the user can always open the swf with a editor to see how it works. So if security is demanded, you cannot save any passwords or user names in the swf itself. But you can for example send the user-password pair to the server, which checks if they are right.

Thanks for the post by the way ;)

Anonymous said...

uRock!

Anonymous said...

Hey guys!

Yeh that's a cool post. I've got a question though, anyone knows if it is possible to add a “separator” as a menu item?

Thanks!

Graham said...

Thanks, I found this incredibly useful since I am still getting into AS3.

Anonymous said...

Nice, thanks for that useful article!

Anonymous said...

Thank you!