Search code examples
javascriptsalesforce-lightning

Not able to disable right-click in Salesforce Lightning Page


The problem is simple if done in a html page using javascript/jquery. But not working in a Salesforce Lightning Page. I want to disable a ui:inputText but not able to do it since it doesn't support contextmenu event. I am able to detect the right click correctly but failing to disable it using mousedown event and return false.

Here is my code below

Page

<ui:inputText aura:id="phid" class="input-data phoneTxt" value="{!v.phno}" keydown="{!c.checkKey}" maxlength="10"
                      cut="{!c.showresult}" mousedown="{!c.showresult}" updateOn="cut keydown keyup keypress mousedown"  />

Controller

preventAction : function(component, event, helper) {
    //console.log(event.getParams('button'));
    console.log(event.getParams());
    if (event.getParams().domEvent.button==2){
        //alert("Right Click is not Allowed");
        //event.getParams().domEvent.preventDefault();
        //event.getParams().domEvent.stopPropagation();
        event.getParams().domEvent.returnValue = false;
    }
}

Note: I was previously trying to disable the right-click cut, copy & paste but thought of disabling only the right-click should solve the problem.

Please suggest some solution to this problem.

Thanks in advance.


Solution

  • Hi this one worked for me. In component add this,

      <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    

    In controller.js add this

      doInit : function(component, event, helper) {
           document.addEventListener('contextmenu', event => event.preventDefault());
      },