46

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

Rismo
  • 6,117
  • 9
  • 34
  • 33
  • I'm afraid. What are you trying to do ? I think depending on the right click and paste has serious web usability issues. – Guido Jan 14 '09 at 08:38
  • I'm confused. Do you mean "detect a right click followed by paste" or "detect a right click, then do a paste"? – Sietse Jan 14 '09 at 08:41
  • Sorry about that. I changed the title and added more text to make it easier to understand. – Rismo Jan 14 '09 at 20:11
  • @Guido García: I'm using the ajax control toolkit autocomplete extender, the problem is that it doesn't autocomplete when you paste text using the mouse on IE (works on Firefox and using ctrl+v works on both) so I'm trying to catch the event to fire the autocomplete. Does this make sense? – Rismo Jan 14 '09 at 20:25

8 Answers8

79

I like this solution:

$('#txt_field').bind('input propertychange', function() {
   console.log($(this).val());
});
oddtwelve
  • 987
  • 8
  • 15
23
$('#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
       */
    }
});
TabLeft
  • 231
  • 2
  • 2
  • Quirksmode says this is a pretty solid solution: http://www.quirksmode.org/dom/events/cutcopypaste.html – marksyzm Jun 24 '13 at 16:17
9

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

Natrium
  • 29,076
  • 15
  • 55
  • 71
epascarello
  • 185,306
  • 18
  • 175
  • 214
  • Can you please provide a full example for both IE and Firefox? Thank you. Also, what about Chrome and Safari? – thedp Dec 23 '09 at 12:48
5

Use setTimeout(), set small timeout till .val() func can get populated.

$(document).on('paste blur keyup', '#controlId', function(event) {
    var element = $(event.target);
    setTimeout(function() {
        var text = $(element).val();
        // do something with text
    }, 100);
});

Source: Catch paste input

Community
  • 1
  • 1
Malik Shahzad
  • 5,446
  • 3
  • 36
  • 45
0

I had the same problem in IE8. Chrome was allowing me to recognize a right click paste, but IE8 was not.

I was able to fix the issue with JQUERY using the mouse leave function as described by Aaron, but here is the code:

for IE8:
    $( "#field" ).mouseleave(function() {
                doStuff());
            });

for Chrome:
    $('#field').bind('input',function() {
                doStuff();
            });
BHOW
  • 129
  • 3
0

I did the following which only fires on mouseup:

onmouseup="jQuery(this).on('paste',function(event){setTimeout(function(){alert('Paste detected!');},100);});"
Bob
  • 55
  • 6
0

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

 <v-text-field 
   @input="$emit('rightclickpaste',$event)"  
 >             
 </v-text-field>
Jared
  • 678
  • 8
  • 12
-1

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

Aaron
  • 1,783
  • 3
  • 19
  • 49