Preventing the right mouse cheat in your Flash games

Today I played the millionth mouse avoider Flash game… Shirk!

I am a real pro at mouse avoider games… look how easily I beat the hard level.

Watch the video!

What a champion…

This happens because you can right click the mouse, showing the menu.

As far as I know, you can’t prevent the menu to show, but you can detect when the player presses the right mouse button.

Just insert into your code, in a place that will be executed every frame, this script:

1
2
3
if (ASnative(800, 2)(2)) {
	// code to execute when the player tries to cheat
}

What is that ASnative function?

It’s an undocumented function used mainly by component developers. You can find some examples at Open Source Flash

Since the documented mouse handlers only detect left mouse button, we need to use this function to do the trick.

But remember this function is undocumented and therefore unsupported.

Moreover, there is no guarantee future versions will support it or work with it

But at the moment it seems to work, and that’s enough.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (9 votes, average: 4.33 out of 5)
Loading ... Loading ...
Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 19 comments

  1. RJ

    on May 10, 2008 at 10:43 pm

    Why not use

    if(Key.isDown(2)){
    //Cheat code
    }

  2. Gabriel Bianconi

    on May 10, 2008 at 11:09 pm

    This is VERY helpful.

  3. Emanuele Feronato

    on May 10, 2008 at 11:44 pm

    it’s the same, but ASnative is more “obfuscated”

  4. Michael

    on May 11, 2008 at 6:35 am

    Hah! Nice one, I’ve never seen or known about that ‘hack’ to be honest; I was only aware of the old Tab trick (which is obviously killed by putting all your code on one frame like you always recommend).

    I don’t see how you could possibly beat those levels without that hack, lol.

  5. plam4u

    on May 11, 2008 at 8:21 am

    Hi Emanuele,
    did u read this:
    http://www.einternals.com/blog/web-development/flashflex/disable-right-click-menu-in-flash

  6. Emanuele Feronato

    on May 11, 2008 at 10:32 am

    yes plam4u, but you need javascript too, and when you publish your games in the portals, they won’t add any JS script

  7. Viktor Hesselbom

    on May 11, 2008 at 3:15 pm

    This won’t trigger on a Mac though. Only on PC (and perhaps Linux, never tried that).

  8. James Prankard

    on May 11, 2008 at 7:38 pm

    I think this is a well used cheat in flash games but your missing the cleverness of the developer.
    If he knows the code to change the right click menu then he can probably put in an anti cheat too. But he specifically kept it in so more users can link back to his site.
    @Michael: there is also code somewhere to disable the inbuilt flash “tab” feature rather than detecting when the user presses tab. I remember reading it somewhere.

  9. Shadow Scythe

    on May 12, 2008 at 1:49 am

    I knew about the Key.isDown(Key(2)) one already. I think that may be more effective. Good find anyways, though.

  10. Kesh

    on May 12, 2008 at 7:51 am

    well, the key(2) is less effective because that only detects if the right click button was pressed. What if someone had a tablet pc and there are no right click buttons? All they have to do is hold down the stylus until it right clicks… how can u detect that with key(2)? I havent tested it, but still Emanuele’s way is much more effective.

  11. Mike

    on May 12, 2008 at 4:31 pm

    Solution: Don’t do mouse avoider games. They suck.

  12. lewis

    on May 12, 2008 at 6:34 pm

    you can change what the right click says and take the clicker toa certain slide.
    Just put:

    function go () {
    gotoAndStop(19);
    }

    var myMenu:ContextMenu = new ContextMenu();
    myMenu.hideBuiltInItems();

    var cheat1:ContextMenuItem = new ContextMenuItem(“Click here for money”, go);
    var cheat2:ContextMenuItem = new ContextMenuItem(“Cheat at the lottery!”, go);
    var cheat3:ContextMenuItem = new ContextMenuItem(“Free pie”, go);
    var cheat4:ContextMenuItem = new ContextMenuItem(“Click here to cheat”, go);
    var cheat5:ContextMenuItem = new ContextMenuItem(“Cheating is for NOOBS!”, go);
    var cheat6:ContextMenuItem = new ContextMenuItem(“Cheating is for gays”, go);
    var cheat7:ContextMenuItem = new ContextMenuItem(“Please don’t cheat”, go);
    var cheat8:ContextMenuItem = new ContextMenuItem(“CHEAT!!!”, go);

    myMenu.customItems.push(cheat1, cheat2, cheat3, cheat4, cheat5, cheat6, cheat7, cheat8);

    _root.menu = myMenu;
    you can change what it says by changing the bit where it says stuff like “CHEAT!!!”
    it is very usefull.

  13. Marcin

    on July 26, 2008 at 3:40 pm

    Well, you could simply put in some code that will say “You’re cheating!” and restart the game if you press the left mouse button. Simply, if you right-click you have to left click to switch off the menu and continue playing…

  14. Graham

    on July 30, 2008 at 4:51 am

    Does anyone know how to do this in AS3?

  15. Javier Lázaro

    on October 30, 2008 at 6:22 pm

    Wonderful!
    Just needed this to fix a cheat on my latest game!

  16. ARK

    on March 23, 2009 at 12:20 am

    There is a way to do this using ActionScript 3.0:

    stage.addEventListener(Event.MOUSE_LEAVE, func);
    function func (event:Event):void
    {
    //the code inside this function will be triggered when the mouse leaved the stage or when the contextual menu (right-click) is triggered
    }

  17. AJ Simon

    on April 26, 2009 at 11:22 pm

    the AS3 solution posted above does not work… i have a client the is going above and beyond requesting to have this for there game. first we never promised it, because if people want to cheat at a game… well then what is the point. people have cheated since the contra game and everyone i am sure remembers that cheat code to get 30 xtra men.

    regardless, i would love to see an AS3 solution for this i have search high and low and found nothing that will meet the need.

    -aj

  18. Josh

    on July 26, 2009 at 5:34 pm

    I would check the location of the movieclip object instead of the mouse x/y position for collision. Then, the right-click cheat shown in that video would actually make you lose.

  19. CTC

    on August 19, 2009 at 1:20 am

    Josh is right, you shouldn’t check the mouse position.
    Instead update xpos/ypos variables from the mouse position on mouse movements and check against those.
    Once you right click the variables don’t get updated and you collide.