I have heard much talk over the last couple years about “Right Click” functionality in Flash. I never paid puch attention to it as I never really needed it ever. Just the other day though it came up yet again and someone linked an article to this guy Uza’s blog who had a JScript for getting the functionality in AS3. The original code project can be found here caught my interest for some reason that I can’t explain. It probably had to do with the game library I’m coding, I figured some developers might actually want this in their games, so why not add it in!?
Unfortunately his script only informed flash of when the right mouse button was pressed over the Flash video. So I tweaked the script some to add release functionality as well.
To make this right click functionality blend well I created a Singleton class that emulates normal Flash MouseEvent protocols. You get a lot of the normal features you may already be used to with left-click all wrapped up nicely in this Singleton.
- MouseEvents are dispatched into the event pool like other events in flash
- MouseEvents are dispatched by the top most InteractiveObject that was right-clicked
- MouseEvents bubble through the DisplayList like usual
- Three Special events are registered and act just like left button events: rightMouseDown, rightMouseUp, and rightClick
- An optional ability to check if the object is mouse-cancelled via its mouseEnabled property or any parents mouseChildren property
The final option may be considered processor intensive in some situations and can be optionally turned on/off. When off then the top most InteractiveObject is just returned as the target no matter if it is enabled or not. Unless you have tons of overlapping InteractiveObjects though, it really shouldn’t matter either way.
So lets introduce you to the script!
All you need to do is import LoDRightMouseButtonTrap into your html document and initialize it. When you initialize it you tell it what SWFObject to pay attention to:
LoDRightMouseButtonTrap.init( “idOfSWFObject”, “idOfFlashContainer” );
This way LoDRightMouseButtonTrap.js knows how to locate the SWF and communicate to it. At this time you can only support one SWF per page at a time. I’ll be changing that in the future of course. But lets get this out the door right now for you guys.
Now lets take a look at the Singleton that handles this thing:
There it is, yes it’s a little long. But that’s ok, it all runs fine and for you to utilize it is really freakin’ easy.
RightClickManager.instance.start( this.stage );
And there, it’s up and running. Keep in mind, the SWF MUST be running in an html container and have the .js script running at the same time. If this isn’t settled then it doesn’t work at all. So no, this does not work on locally running SWFs via a projector. If you want local running access, run it via a browser locally, OR just create an AIR app which gives you RightClick functionality out of the box. That was one of my drives for writing this, if you wanted to port your internet app to an AIR app, you just don’t start up the RightClickManager and just keep on going. The events are dispatched with the same MouseEvents that AIR would be dispatching… actually seeing as the manager check for the ExternalInterface at “start”, if it isn’t there, it just doesn’t run. So really you don’t have to change ANYTHING to port an app using this to AIR.
So who wants an example!?
Here I draw 3 boxes named child1 through child3, and stick them in various containers, then I listen to the root for right clicks and display some text for it. Note how everything bubbles and the correct displayObject is picked up here in the example running:
Please check out my game framework for the classes needed. You are intereseted in:
com.lordofduct.ui.RightClickManager – this is the actual manager
com.lordofduct.util.Assertions – needed for nil checking
com.lordofduct.util.SingletonEnforcer – needed to assert the Singleton instance
The latest version of it is available in the right hand link bar.