본문 바로가기

ActionScript/AS3.0

Right Click in AS3

자바스크립

  1. /**
  2. *
  3. * Copyright 2007
  4. *
  5. * Paulius Uza
  6. * http://www.uza.lt
  7. *
  8. * Dan Florio
  9. * http://www.polygeek.com
  10. *
  11. * Project website:
  12. * http://code.google.com/p/custom-context-menu/
  13. *
  14. * --
  15. * RightClick for Flash Player.
  16. * Version 0.6.2
  17. *
  18. */
  19.  
  20. var RightClick = {
  21.     /**
  22.      *  Constructor
  23.      */
  24.     init: function () {
  25.         this.FlashObjectID = "customRightClick";
  26.         this.FlashContainerID = "flashcontent";
  27.         this.Cache = this.FlashObjectID;
  28.         if(window.addEventListener){
  29.              window.addEventListener("mousedown", this.onGeckoMouse(), true);
  30.         } else {
  31.             document.getElementById(this.FlashContainerID).onmouseup = function() { document.getElementById(RightClick.FlashContainerID).releaseCapture(); }
  32.             document.oncontextmenu = function(){ if(window.event.srcElement.id == RightClick.FlashObjectID) { return false; } else { RightClick.Cache = "nan"; }}
  33.             document.getElementById(this.FlashContainerID).onmousedown = RightClick.onIEMouse;
  34.         }
  35.     },
  36.     /**
  37.      * GECKO / WEBKIT event overkill
  38.      * @param {Object} eventObject
  39.      */
  40.     killEvents: function(eventObject) {
  41.         if(eventObject) {
  42.             if (eventObject.stopPropagation) eventObject.stopPropagation();
  43.             if (eventObject.preventDefault) eventObject.preventDefault();
  44.             if (eventObject.preventCapture) eventObject.preventCapture();
  45.          if (eventObject.preventBubble) eventObject.preventBubble();
  46.         }
  47.     },
  48.     /**
  49.      * GECKO / WEBKIT call right click
  50.      * @param {Object} ev
  51.      */
  52.     onGeckoMouse: function(ev) {
  53.         return function(ev) {
  54.         if (ev.button != 0) {
  55.             RightClick.killEvents(ev);
  56.             if(ev.target.id == RightClick.FlashObjectID && RightClick.Cache == RightClick.FlashObjectID) {
  57.                 RightClick.call();
  58.             }
  59.             RightClick.Cache = ev.target.id;
  60.         }
  61.       }
  62.     },
  63.     /**
  64.      * IE call right click
  65.      * @param {Object} ev
  66.      */
  67.     onIEMouse: function() {
  68.         if (event.button> 1) {
  69.             if(window.event.srcElement.id == RightClick.FlashObjectID && RightClick.Cache == RightClick.FlashObjectID) {
  70.                 RightClick.call();
  71.             }
  72.             document.getElementById(RightClick.FlashContainerID).setCapture();
  73.             if(window.event.srcElement.id)
  74.             RightClick.Cache = window.event.srcElement.id;
  75.         }
  76.     },
  77.     /**
  78.      * Main call to Flash External Interface
  79.      */
  80.     call: function() {
  81.         document.getElementById(this.FlashObjectID).rightClick();
  82.     }
  83. }
액션스크립트
  1. package {
  2.    
  3.     import flash.display.*;
  4.     import flash.external.ExternalInterface;
  5.  
  6.     public class RightClick extends Sprite
  7.     {
  8.        
  9.         public function RightClick()
  10.         {
  11.             stage.scaleMode = StageScaleMode.NO_SCALE;
  12.             stage.align = StageAlign.TOP_LEFT;
  13.            
  14.             var methodName:String = "rightClick";
  15.             var method:Function = onRightClick;
  16.             ExternalInterface.addCallback(methodName, method);
  17.         }
  18.        
  19.         private function onRightClick():void {
  20.  
  21.             var mx:int = stage.mouseX;
  22.             var my:int = stage.mouseY;
  23.  
  24.             if(my> 0 && my <stage.stageHeight && mx> 0 && mx <stage.stageWidth) {
  25.                 // YOUR CODE HERE
  26.             }
  27.         }
  28.     }
  29. }

출처

demo url : http://www.uza.lt/rightclick/
project url : http://code.google.com/p/custom-context-menu/
original post : http://www.uza.lt/blog/2007/08/solved-right-click-in-as3/