Patternhawk Enterprises: FLEX / PHP/ mySQL / HTML / JAVA  Development Lessons and Tips

Patternhawk Enterprises Inc.

Flex / PHP etc.

Patternhawk - Home | Pilot Information | Patternhawk Information | Dash-8 Defender

3. Right Click Disable

While vigorously play-testing one day, I accidently hit the right mouse button in the middle of a heated engagement.  Needless to say, when the adobe context menu came up, it wreaked havoc with my battle solution and I soon wound up assimilated.  There was a definite requirement to disable the right mouse button while in the middle of the game.  Unfortunately, Flex provides no way to do this.

Fortunately; however, there are many smart people out there who come up with work-arounds.  Some of them are over at the ADM Blog who have the solution.

In a nutshell, you put a Javascript function that kills the right click on your web page with the Flex object, then make sure that you have some Actionscript code that allows your Flex object to communicate with this Javascript function.  The neat thing is, the right click is only disabled while over your Flex object!  My implementation looks like this.

Solution:

Javascript Code

<script language="JavaScript" type="text/javascript">
var RightClick = {
	init: function () {
		this.FlashObjectID = "dash";
		this.FlashContainerID = "myFlashContent";
		this.Cache = this.FlashObjectID;
		if(window.addEventListener){
			 window.addEventListener("mousedown", this.onGeckoMouse(), true);
			 document.oncontextmenu = function() { document.getElementById("dash").rightClick(); }
		} else {
        
			document.getElementById(this.FlashContainerID).onmouseup = function() { document.getElementById(RightClick.FlashContainerID).releaseCapture(); }
			document.oncontextmenu = function(){ if(window.event.srcElement.id == RightClick.FlashObjectID) { return false; } else { RightClick.Cache = "nan"; }}
			document.getElementById(this.FlashContainerID).onmousedown = RightClick.onIEMouse;
		}
	},
	killEvents: function(eventObject) {
		if(eventObject) {
			if (eventObject.stopPropagation) { eventObject.stopPropagation(); }
			if (eventObject.preventDefault) { eventObject.preventDefault(); }
			if (eventObject.preventCapture) { eventObject.preventCapture(); }
		    if (eventObject.preventBubble) { eventObject.preventBubble(); }
		}
	},
	onGeckoMouse: function(ev) {
		return function(ev) {
		if (ev.button != 0) {
			RightClick.killEvents(ev);
			if(ev.target.id == RightClick.FlashObjectID && RightClick.Cache == RightClick.FlashObjectID) {
                document.getElementById(RightClick.FlashObjectID).rightClick();
			}
			RightClick.Cache = ev.target.id;
		}
	  }
	},
	onIEMouse: function() {
        if (document.getElementById(RightClick.FlashObjectID + 'x'))
            document.getElementById(RightClick.FlashObjectID + 'x').id =  RightClick.FlashObjectID;
            
		if (event.button> 1) {
			if(window.event.srcElement.id == RightClick.FlashObjectID && RightClick.Cache == RightClick.FlashObjectID) {
				RightClick.call();
			}
			document.getElementById(RightClick.FlashContainerID).setCapture();
			if(window.event.srcElement.id)
			RightClick.Cache = window.event.srcElement.id;
		}
	},
	call: function() {
		document.getElementById(RightClick.FlashObjectID).rightClick();
	}
}
</script>               
                
      
AS Code That Sits in Your Flex Object 
         
private function rightinit() : void
  {
  ExternalInterface.addCallback("RightClick", onRightClick);
  }
			
private function onRightClick():void 
  {
  var mx:int = stage.mouseX;
  var my:int = stage.mouseY;
				
  if(my> 0 && my < stage.stageHeight && mx> 0 && mx < stage.stageWidth) 
    {       // this ties into the java script to do nothing when the right mouse is clicked
    }       // You could theoretically put anything you want in here.  I just wanted it to do nothing.
  }

Remember to Put in the Header of Your Flex Code
        
<mx:Application 
  ...
  initialize="rightinit()">    
                

Now for completeness, this is the standard code which embeds the Flex object. Note that the ID's have to match up with the FlashObjectID and the FlashContainerID in the Javascript function, but only for the IE object. The internal Firefox object is covered by the external IE object. At least that's what I figure since it doesn't matter what you give for an ID for the Firefox object.

HTML Code

<div id="myFlashContent">
  	            
  <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="800" height="600" id="dash">   <!-- for IE -->
    <param name="movie" value="yourflex.swf" />
    <param name="menu" value="false">
    <param name="wmode" value="opaque" />
    <!--[if !IE]>-->
    <object type="application/x-shockwave-flash" data="yourflex.swf" width="800" height="600" id="das">        <!-- for Firefox -->
	<param name="menu" value="false">
  	<param name="wmode" value="opaque" />
    <!--<![endif]-->
	<a href="http://www.adobe.com/go/getflashplayer">
	<img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" />
	</a>
    <!--[if !IE]>-->
	</object>
    <!--<![endif]-->
  </object>
		        
</div>


Remember to Put This in the BODY Command of Your HTML...

<BODY ... onload="RightClick.init();>			
				

Note that the settings for menu and wmode must be applied for the IE and Firefox objects. Also note that when you set the wmode this way, you disable the right click, but the unfortunate side-effect is that it interrupts the pre-loader and makes it skip. Oh well, can't have everything I guess.

1 2 3

 

 

Bookmark This Site

Main Menu

 Home

 Pilot Information

 Patternhawk Information

 Dash-8 Defender

Custom Search

 

Copyright 2012  Patternhawk Enterprises   All Rights Reserved