How to detect right mouse click + paste using JavaScript?

Viewed 49366

Is there a way to detect a right click followed by paste with JavaScript on IE and Firefox ?

Update:

I decided to use Jquery to do it:

$('#controlId').bind('paste', null, function() {
    // code
});

It's not exactly what I was looking (because it gets fired on 'ctrl + v' as well as in 'right click + paste' but I can work around it.

Tested it on Chrome, Firefox 3, IE 7 and IE 6 and it's working

8 Answers
$('#controlId').bind('paste', null, function(e) {
    if(!e.keyCode){
       /*
          since no key was down at the time of the event we can assume it was
          from the toolbar or right click menu, and not a ctrl+v
       */
    }
});

With IE you have onpaste

With Mozilla you can look into oninput and

elementReference.addEventListener("DOMCharacterDataModified", function(e){ foo(e);}, false);

There is no easy as pie solution.

Eric

if you're using vue you can emit and pass pasted value like so:

 <v-text-field 
   @input="$emit('rightclickpaste',$event)"  
 >             
 </v-text-field>

A cheap hack (that works) that you can try is:

  • jQuery's mouseleave function.

I've noticed with IE8 that if you right-click in a text box and then select 'paste', it delays the "mouseleave" event until the paste has finished. So it consistently fires right after the paste! :) Works for me and got me out of trouble perfectly actually.

This is only for an intranet app, I haven't tested in Firefox etc.

Cheers

Related