AS3 Quickie – Removing the right-click menu from Flash content

One of the great new features introduced with Flash Player 11.2 is the ability to intercept the right-click from a user’s mouse. A simple MouseEvent listener can be added to the Stage or any object which extends flash.display.InteractiveObject. Not only does this have the effect of completely removing the right-click menu from Flash content, but we can also alternatively react to the right-click in any way we choose.


Requires Flash Player 11.2 or above!

To enable this, we must import the MouseEvent class into our project:

import flash.events.MouseEvent;

Next, let's add a listener to the Stage itself, disabling the context menu in the process:

stage.addEventListener(MouseEvent.RIGHT_CLICK, doNothing);

In the included example, we also register a listener upon our MovieClip symbol. A right-click upon this will fire off an additional method which controls the animation:

angryFace.addEventListener(MouseEvent.RIGHT_CLICK, destroyFace);

That's it! A pretty simple solution for a problem that Flash developers have had pretty much since Flash was Flash :)

Here is the full source code:

package  {
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.display.MovieClip;
	
	public class KillContextMenu extends Sprite {
		
		public var angryFace:MovieClip;
		
		public function KillContextMenu() {
			stage.addEventListener(MouseEvent.RIGHT_CLICK, doNothing);
			angryFace.addEventListener(MouseEvent.RIGHT_CLICK, destroyFace);
		}
		
		private function doNothing(e:MouseEvent):void {
			//do nothing...
			trace(e.type);
		}
		
		private function destroyFace(e:MouseEvent):void {
			//send to oblivion
			e.currentTarget.gotoAndPlay("Destruction");
		}
		
	}
}

Want the full package? Download below:
Packaged code and Flash Professional CS6 project

12 comments

  1. Thank you for this article. I have one question : Is it possible to use this with a link on the animation. i tried but i’m not an expert an i got many error codes.

    If it is possible, please let me know how.

    Thank for your help

  2. is it possible to achive the same from AS3 in flex builder, i had a go but could not get the rigjht click
    angryFace.addEventListener(MouseEvent.RIGHT_CLICK, destroyFace)
    to work

  3. I have the RIGHT_CLICK mouse event and function for that event in main class of flash project. I create the event using stage.addEventListener(MouseEvent.RIGHT_CLICK, M2), where M2 is function which shall do something after right click. After doing this, flash right click menu gets disabled, but the function dont do anything else. Is there a way to fix it? I am using adobe flash, project flash player is 11.2.

  4. I have a project I am trying to turn off the right click. Can you be of any assistance.

    Thanks

    Mike

  5. I was seeing the same issue as A3l, when running the script (including yours) in flash the function doesn’t appear to do anything, but once it’s published it works fine.

Comments are closed.