SOLVED: Right Click in AS3
August 19th, 2007Eureka,
A day or two ago polyGeek has revived an old and challenging idea that one could make use of custom right-click functionality in Flash (AS3 + Javascript).
Why would anyone want to do this? Well, there are several very important reasons:
1) Games – the power of AS3 has brought Flash to the world of digital entertainment. At last it is possible to focus on the idea of your game rather than on how to improve the laggy experience. One thing that is still missing – right click functionality. We had this forever in desktop games, now it is time to let your casual RTS, RPG and FPS creations conquer the web.
2) User Experience – 2 buttons are better than 1. Every experimentalist's dream is to be able to have more input options, not just one button. I can bet someone would soon create a stunning interface using this new functionality and we would see that on no less than FWA.
3) RIA – Rich Internet Applications. My clients are often asking if it is possible to remove embeded Flash Player menus from their applications and replace them with their company’s branding stuff.
***
AND THE ANSWER IS – YES! It is now possible to use custom right-click functionality in Flash and even Flex.
After long hours of searching through Microsoft’s documentation I came up with a universal solution that works nicely at least on 3 major browsers – Firefox 2, IE 7 and Safari. (IE6 has not been tested but I can bet it works OK).
***
Here you can see the * DEMO of right click * functionality (click the grey area to draw transparent dots)
Javascript source code looks like this:
-
/**
-
*
-
* Copyright 2007
-
*
-
* Paulius Uza
-
* http://www.uza.lt
-
*
-
* Dan Florio
-
* http://www.polygeek.com
-
*
-
* Project website:
-
* http://code.google.com/p/custom-context-menu/
-
*
-
* --
-
* RightClick for Flash Player.
-
* Version 0.6.2
-
*
-
*/
-
-
var RightClick = {
-
/**
-
* Constructor
-
*/
-
init: function () {
-
this.FlashObjectID = "customRightClick";
-
this.FlashContainerID = "flashcontent";
-
this.Cache = this.FlashObjectID;
-
if(window.addEventListener){
-
window.addEventListener("mousedown", this.onGeckoMouse(), true);
-
} 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;
-
}
-
},
-
/**
-
* GECKO / WEBKIT event overkill
-
* @param {Object} eventObject
-
*/
-
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();
-
}
-
},
-
/**
-
* GECKO / WEBKIT call right click
-
* @param {Object} ev
-
*/
-
onGeckoMouse: function(ev) {
-
return function(ev) {
-
if (ev.button != 0) {
-
RightClick.killEvents(ev);
-
if(ev.target.id == RightClick.FlashObjectID && RightClick.Cache == RightClick.FlashObjectID) {
-
RightClick.call();
-
}
-
RightClick.Cache = ev.target.id;
-
}
-
}
-
},
-
/**
-
* IE call right click
-
* @param {Object} ev
-
*/
-
onIEMouse: function() {
-
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;
-
}
-
},
-
/**
-
* Main call to Flash External Interface
-
*/
-
call: function() {
-
document.getElementById(this.FlashObjectID).rightClick();
-
}
-
}
On the Flash side is as simple as this code (AS3):
-
package {
-
-
import flash.display.*;
-
import flash.external.ExternalInterface;
-
-
public class RightClick extends Sprite
-
{
-
-
public function RightClick()
-
{
-
stage.scaleMode = StageScaleMode.NO_SCALE;
-
stage.align = StageAlign.TOP_LEFT;
-
-
var methodName:String = "rightClick";
-
var method:Function = onRightClick;
-
ExternalInterface.addCallback(methodName, method);
-
}
-
-
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) {
-
// YOUR CODE HERE
-
}
-
}
-
}
-
}
This demo has been tested and confirmed working on:
WINDOWS VISTA
- Internet Explorer 7.0.6001 (16549)
- Firefox 2.0.0.6 (with mouse gestures disabled)
- Maxthon 2 (with mouse gestures disabled)
- Safari 3.0.3 (522.15.5)
Windows XP SP2
- Internet Explorer 6
- Internet Explorer 7
- Maxthon 2 (with mouse gestures disabled)
- FireFox 2 (with mouse gestures disabled)
- Safari 3
- Netscape 8
Mac OSX 10.4.10 (Intel)
- Firefox 2
- Safari 3.0.3
Thank you all for testing!
Opera will not work, the browser forces the context menu to appear and blocks mouse events by default.
If you manage to get the demo working, please post the OS and Browser build number in the comments. Please also leave a comment if you experience any problems with the demo.
Google Code Project
DEMO
DIGG THIS
.

August 19th, 2007 at 8:12 am
Not work for Linux?
August 19th, 2007 at 10:41 am
Mac 10.4.10 (Intel) + FF 2.0 = true
Mac 10.4.10 (Intel) + Safari 2.0 = false
August 19th, 2007 at 10:46 am
No love from Safari (Version 2.0.4 (419.3)) under Mac 10.4.10 under PPC either.
August 19th, 2007 at 12:53 pm
Finally a working solution
Vista + Firefox 2.0.0.6 = true
August 19th, 2007 at 1:25 pm
The problem with this solution is that you use wmode="opaque" or wmode=transparent" to let the browser use its own windowing mode on the animation. There is bugs with Firefox on international keyboards when typing special symbols, @ sign and some other keys in input fields when wmode is set other than windowless. On French keyboard when you type @sign you will have the letter "à" in the input field.
Setting wmode to opaque is also know to slightly reduce framerate on animations that use a lot of graphical rendering (wmode transparent reduce it noticeably).
August 19th, 2007 at 1:29 pm
My list as tested on WinXP SP2:
Internet Explorer 6: works
Internet Explorer 7: works
FireFox 2: works
Opera 9 : doesn't work
Safari 3: works
Netscape 8: works
August 19th, 2007 at 1:34 pm
[...] immer das Context-Menü des FlashPlayers, welches man auch selbst noch erweitern kann. Aber gibt es hier eine Lösung um die Rechts-Klick -Funktionalität selbst zu bestimmen. Die Lösung ist eine Kombination aus [...]
August 19th, 2007 at 1:43 pm
Nice idea, but it works on all the page and not only over the Flash, a bad usability idea...
August 19th, 2007 at 1:43 pm
Not working on Linux (xubuntu) with either Firefox or Opera.
August 19th, 2007 at 1:44 pm
P.S.: Both browser are the latest stable versions.
August 19th, 2007 at 5:18 pm
[...] Right Click In Flash http://polygeek.com/377_adobeflash/actionscript_custom-right-click-in-flash http://www.uza.lt/blog/2007/08/solved-right-click-in-as3/ Många har gett sig på högermenyn i flash, detta är ett av få exempel där menyn inte bara tas [...]
August 19th, 2007 at 5:41 pm
Thanks to everyone for your comments!
Ahmet: I have disabled the all-page effect by removing "oncontextmenu" events, thanks for pointing this out
Matthias: I will try to get a linux box to test this out, thanks. Opera will not work, the browser forces the context menu to appear and blocks mouse events by default.
Tek: I haven't thought about this, I will take a look, thanks.
August 19th, 2007 at 7:41 pm
Thanks for picking up the torch on this Paulius. I've added you as an owner on the Google Code project that I started. I'm sure that a reliable solution will be found and the Flash community will be able to take over the Flash context-menu!
August 19th, 2007 at 10:01 pm
Doesn't work in Mac OS X Safari or FF (EDIT BY UZA: Actually it works on the latest FF 2.0.0.6 on Mac OS X 10.4 Intel). Looks like Paulius is feeling the same pain as polyGeek - it also highlights exactly why Flash/Flex AS,2,3 are so superior to other scripting languages. At least in Flash you KNOW your stuff works in all browsers on 3 platforms. I have no idea how Google does it, they must have a kick-ass QA dept.
This will rock if it ever works.
August 21st, 2007 at 8:17 am
Context menu still appears in FF 2.0.0.6. Could be a mouse gestures issue?
August 21st, 2007 at 9:52 pm
MG: Yes, this IS a Mouse Gesture issue. I can confirm that this solution does not work on browsers that have Mouse Gestures enabled.
Such browsers are Opera, Mozilla Firefox (with plugin) and Maxthon (with plugin).
August 23rd, 2007 at 5:28 pm
Firefox 2.0.0.6 on MacOS X 10.4.8 -- work.
Safari 2 on MacOS X 10.4.8 -- not work
August 24th, 2007 at 12:09 am
[...] На блоге uza - интересная попытка совсем убрать стандар... [...]
August 25th, 2007 at 4:11 am
[...] last couple of days have been pretty busy for me trying to make some progress with the Right-Click functionality in Flash 9, it is coming together pretty good and I hope to have a working Linux version very [...]
August 25th, 2007 at 5:21 pm
Windows XP SP2 + IE 6.0.2900.2180 No
Windows XP SP2 + Firefox 2.0.0.6 Yes Yes Yes !!!!!!!!
August 27th, 2007 at 4:01 pm
It's not working for me on vista and firefox 2.0.0.6. If you rightclick in the gray area it shows the traditional context menu. If you click left on the flash area first, and then click right you get the same context menu like when you make a selection.
August 27th, 2007 at 4:03 pm
As someone who's dealt with how browser plugins are implemented on X11 (Linux/Unix), I'd be surprised if this ever works there any time soon. Not your fault, though.
It's a major hack just to get page content to even overlap Flash on X11 using the normal Netscape plugin architecture. Basically, the plugin is going to sit on top of your page, catching events regardless of its z-index or what you're trying to hook. You can think of it as like trying to capture events on a page while your email client is in front of the browser.
It'd take something like the redirected drawing stuff Zack Rusin is working on to make this nicer. He's talking in terms of Qt/KDE/WebKit, but the same idea would apply to Firefox/X11: http://zrusin.blogspot.com/2007/06/mirroring-widgets.html
August 27th, 2007 at 4:59 pm
[...] caratteristica sarebbe interessante sia per i giochi che per le Applicazioni RIA. Paulius Uza ha proposto una soluzione per ActionScript 3 (AS3) che lavora in accoppiata con Javascript. Questa “patch” è ancora in fase di [...]
August 27th, 2007 at 10:56 pm
[...] of all I would like to thank everyone for their great feedback regarding the Right-Click project. I have received thousands (!!!) of feedback emails with your browser data which helped me to [...]
August 28th, 2007 at 2:41 am
[...] Uza’s Blog & More » Blog Archive » SOLVED: Right Click in AS3 (tags: actionscript flash as3 click flex right web development webdesign) [...]
August 28th, 2007 at 10:08 am
Windows XP SP2 + IE 6.0.2900.2180 Yes Yes Yes
i has flash player problem
September 19th, 2007 at 5:58 am
It works well on IE6 and firefox2.
I look forward to complete rightmouse event,like RIGHT_MOUSE_DOWN,RIGHT_DOUBLE_CLICK.
Thank you for this!
October 7th, 2007 at 6:46 am
http://johnjddeservio.com/forum/viewtopic.php?p=7315
October 7th, 2007 at 10:24 am
http://www.thepeppermint.com/phpBB2/viewtopic.php?p=54030
October 7th, 2007 at 11:43 am
http://wirelessfm.org/cirg/viewtopic.php?p=35524&sid=6753dbea77be755a60a33e3105d9da58
October 7th, 2007 at 1:11 pm
http://omcity.free.fr/forum/viewtopic.php?p=4604
October 26th, 2007 at 2:05 pm
Does not work on Firefox 2.0.0.8 on Windows XP SP2. Draws circle but context menu comes up when right-clicking on flash box. I do not use gesture plugins, but a few plugins I use do add options to the context menu, and I wonder if this is an issue since others have said Firefox 2.0.0.6 works...
November 30th, 2007 at 3:58 pm
Works for me.. (XP Firefox 2 flash 09.r60 )
What's Adobe position about this ?
Is it..ya know...legal ?
November 30th, 2007 at 4:09 pm
@Adrian:
Adobe has expressed some interest into evolving right-click functionality in the future versions of Flash Player through unofficial channels, but no official statement has been made.
From the legal side - this "hack" uses 100% standards compliant JavaScript and communicates with Flash Player through a bridge provided by Adobe - so yes, it is legal.
December 10th, 2007 at 7:08 pm
[...] way of dealing with the lack of full support for "right click" events in Flash. Check out this blog posting for more info. Another good use of ExternalInterface [...]
December 28th, 2007 at 2:15 pm
[...] 可以看看介绍:http://www.uza.lt/blog/2007/08/solved-right-click-in-as3/,效果这里可以看到:http://www.uza.lt/rightclick/,代码这里可以下载到:http://code.google.com/p/custom-context-menu/。 PLAIN TEXT HTML: [...]
December 31st, 2007 at 3:47 pm
Hi Guys,
it does not work in Safari 3.0.4.
Can you look at it please?
Thanks
Tom
January 4th, 2008 at 10:03 pm
Hey!
I've just an hour ago got in from work, where today I had written a "right clicker" for AS3, and would you believe it - I scan a little on Google and I find this!
A couple of notes!
1] Opera: Press Ctrl+F12, or click to the preferences... choose "Content" and find the "JavaScript options..." button, can you believe it? Why on earth did they do that?
2] With that disabled you have a new problem, just disable the darn flash context menu the usual way... (in script/on publish)
There you have it, the definitive right click solution!
PS: Put the JS in a and then use ExternalInterface.call("eval", ...) to inject it into the page, now you have a single solution! Another idea would be to dispatchEvent(MouseEvent.RIGHTCLICK...); - the right click event is there for Air applications, so now you have unified your in-browser/desktop code without compromising functionality.
Oh and Happy New Year!
)
January 4th, 2008 at 10:05 pm
note: where it says "PS: Put the JS in a and..."
It should say CDATA...
January 4th, 2008 at 10:11 pm
PPS: *rollseyes*
RE (anyone with a browser it won't work in):
Another way to have right click is to create a 'div' and 'spacer.gif' to sit above the flash 'div'...
Handling ordinary flash mouse events is a bit of a longhaul JS-AS hack but it works, you can use a cheap dispatchEvent or better still swap the z-index/show-hide the div... its a pain to implement but onces its wired up its good to go.
January 7th, 2008 at 1:54 pm
These Script working in only Cs3 or Flash 8 and mx Proffessional also Pls Confirm.There is no working in Flash 8.I am Checked.Pls Clarify in any clarifications and modify in that code working to flash 8.because my project is already working in flash 8 and mx proffessional.I am waiting for ur reply.
Thank u
January 11th, 2008 at 7:00 am
Cool good work. Just interested if there is a solution for Flash8
March 11th, 2008 at 2:22 pm
great work by you... thanks a lot..
the code what u gave for the external interface calls error in flash can u explain how to apply this code of action script to call the external interface?
April 1st, 2008 at 10:52 am
FullScreen can not?
April 4th, 2008 at 11:16 am
you must be talking about the dreaded wmode bug in firefox.
http://www.5etdemi.com/blog/archives/2005/06/firefox-wmodetransparent-is-completely-screwy-and-breaks-textfields/
also look at the 5th response to this blog
April 10th, 2008 at 7:35 pm
Hello,
Is there a possibility to tell a flash- and javanoob like me, how to implement this?
I have an index.htm and a ...swf.
Should I paste the script in the index.htm or call a script in the index?
I hope someone will help me...
May 2nd, 2008 at 9:34 pm
gioca a poker on line...
As a summary of telecharger jeu de poker gratuites payday loan cash advance loan...
May 3rd, 2008 at 11:02 am
kostenlose online spiele casino 14 casino spiele online spielen...
At the beggining card casino free game online payday cash loan...
May 3rd, 2008 at 5:23 pm
jeux gratuits du casino jeux gratuits casino telecharger jeux casino gratuits...
There casino en francais casino et bonus...
May 3rd, 2008 at 7:57 pm
le casino gratuites sans dépot...
You jeu poker texas holdem gratuites poker pc spiel download...
May 4th, 2008 at 6:03 pm
bonus dei casino...
There poker de ases fast cash payday loan...
May 4th, 2008 at 6:33 pm
poker en linea...
Consider advance cash loan loan payday table de poker en ligne...
May 4th, 2008 at 11:41 pm
poker in tour...
Preview jouer poker sur internet jeu slots en ligne...
May 7th, 2008 at 5:47 am
jugar slots en linea...
No cash advance payday loan instant loan online payday...
May 18th, 2008 at 5:25 am
rightClick outside the flash,
html menu appears,
click anywhere outside the htmlMenu to remove the htmlMenu,
now rightClick on the Flash,
first rightclick will not be activated.
anyway, thx a Million, very coOol;
May 26th, 2008 at 7:42 pm
@Ben
that eval trick === _#$c%in'_ hot.
many thanks to uza and the rest of the testers and contributers!
Adobe's conservative stance is somehow understandable... but i totally agree that we are entering an age of multitouch displays... and flash player embedded in a browser is _ONLY_ giving us one button??? lame.
but with an adaptation of this solution, together with swfMacMouseWheel... we can have cross platform, 3 button mouse with scroll wheel action!
this rocks!
~gotjosh
ps (i hope i can convince my boss that we ought to release our solution to y'all)
June 5th, 2008 at 12:06 pm
world champion poker...
Verdad play seven card stud poker sign up bonus juegos azar portales internet top poker promociones jouer video poker...
June 21st, 2008 at 11:36 pm
I posted a topic about right click on the Adobe forum here: http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?forumid=72&catid=675&threadid=1372314&enterthread=y
Perhaps you'd like to offer suggestions or voice your opinion over there as well.
June 25th, 2008 at 6:12 pm
Vista Ultimate 64bit + Firefox 3 + Flash 9 == true
October 6th, 2008 at 8:43 pm
Not work for Solaris 10.....
Mozilla & FireFox, all failed.
December 18th, 2008 at 10:58 pm
I found a way to remove it and that's work on all browser but we need to hack a file. If somebody has more skill to make a plugin with this, would be really nice. Take care because this will remove the right click on all flash movie on Web. For a local game this is perfect.
http://membres.lycos.fr/micban/
January 9th, 2009 at 12:34 pm
WOW!!! greatss ITS WORKINGGG..
February 6th, 2009 at 7:17 am
Great,
February 6th, 2009 at 11:57 pm
I have tried using this with swfObject 2.0 and it worked fine in.
Mac FF 3.06
but it didn't in
PC IE 6
downgrading to swfObject 1.5 made it work across the board.
Any plans of making it work with swfObject 2.0 ?
good work, thanks.
February 7th, 2009 at 11:03 pm
[...] You can download the file : RightClick_0[1].6.2.zip Nice to Visit : http://www.uza.lt/blog/2007/08/solved-right-click-in-as3. [...]
March 4th, 2009 at 2:39 pm
Thanks, this works fantastic!!
The only problem is on the Mac with a single button mouse.
Due to pressing the Ctrl key the mouse event can't be triggert. (Safari 3, Mac OS X)
Does anyone knows a solution for this?
March 25th, 2009 at 11:41 am
In my Opera it does work but the context menu appears anyway. Any ideas to fix that?
April 13th, 2009 at 6:53 pm
I echo Nielsio's response. I'm really looking for a way to disable the right click menu in safari based browsers (webkit) with the control click. Please let me know if anybody has any ideas.
May 23rd, 2009 at 6:40 am
Текст лично мне ничего особенного не дал. Но вот для многих он может оказаться полезным...
May 25th, 2009 at 7:59 pm
Hey this is great stuff. but right now im suffering with the same problem as Tek. the wmode issue. cant use anything other than wmode="window", it messes the keyboard input on firefox/chrome. Any help on this will be greatly appreciated.
May 28th, 2009 at 12:24 am
Нерациональное употребление материальных благ зачастую является вернейшим путем к величайшим невзгодам.
June 9th, 2009 at 3:39 pm
Doesn't work with Safari 4 (final) under OS X 10.5.7 =/
June 17th, 2009 at 6:06 pm
How can i get this for a Full Screen project?
July 8th, 2009 at 9:33 am
hey i really need to have a copy of ur sample code which includes the drawing of dark circles, i have some problem in making it work
July 13th, 2009 at 8:59 am
[...] a method to completely disable the flash right menu. Check the article link at his website http://www.uza.lt/blog/2007/08/solved-right-click-in-as3 and the Demo here [...]
July 15th, 2009 at 4:19 pm
MAC OS 10.5.7
Firefox & SWFObject v1.5 = true;
Safari & SWFObject v1.5 = false;
Firefox & SWFObject v2 = false;
Safari & SWFObject v2 = false;
July 15th, 2009 at 4:19 pm
Safari 4 & firefox 3.5
July 20th, 2009 at 1:22 pm
how to use both code?
July 21st, 2009 at 7:46 pm
I can confirm that google chrome works as well. great job.
July 26th, 2009 at 6:35 pm
Is there really no chance to prevent right clicks for other wmode? We have to use FP10 "direct mode" to reach an acceptable frame rate in our game.
Unfortunately, opening the standard FP menu with a right click, will occasionally freeze the whole browser (FF/IE) until you change to another application and then reactivate the browser window again.
July 27th, 2009 at 4:03 pm
Hey it's not working on Safari (Mac)
http://code.google.com/p/custom-context-menu/issues/detail?id=10
July 30th, 2009 at 8:46 am
[...] an article to this guy Uza’s blog who had a JScript for getting the functionality in AS3. The article found here caught my interest for some reason that I can’t explain. It probably had to do with the game [...]
August 5th, 2009 at 4:11 pm
This works on Google Chrome too. Excellent!
September 23rd, 2009 at 1:37 pm
Awesome, works in Chrome, Firefox and IE 6.0.2900.5512.xpsp_sp3_gdr
October 4th, 2009 at 7:14 am
Got it working with wmode="opaque" but I want to use wmode="direct"... any solution for that?
October 20th, 2009 at 4:53 pm
Working on XP SP2 with Google Chrome (1.0.154.48)
October 28th, 2009 at 5:19 pm
[...] a bit of JS. Really useful in game development, as add a new key to the mouse. Here the link with a quick tutorial « pixel bender – setup :: | [...]
November 9th, 2009 at 9:18 pm
worked!!
XP + Safari 4.0.3
"Thanks for your source code"
November 26th, 2009 at 5:29 am
It is possible to focus on the idea of your game rather than on how to improve the laggy experience. Its very true totally agreed with this.
December 4th, 2009 at 6:15 am
how about in action script 2.0??
December 8th, 2009 at 1:54 pm
This is so hackish
. Good job.
December 14th, 2009 at 7:26 pm
Excellent Work!
Would never have thought of it.
December 24th, 2009 at 9:59 am
Can eliminate all right click menu in flash.
Great job.
Thank you very much.
December 30th, 2009 at 9:22 pm
For some reason my browser doesn't display this page correctly...Anyway,
February 4th, 2010 at 2:42 am
c'est interessant , merci pour l'info !