Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect right mouse click + paste using JavaScript?

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

like image 407
Rismo Avatar asked Jan 14 '09 01:01

Rismo


4 Answers

I like this solution:

$('#txt_field').bind('input propertychange', function() {
   console.log($(this).val());
});
like image 64
oddtwelve Avatar answered Nov 15 '22 21:11

oddtwelve


$('#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
       */
    }
});
like image 23
TabLeft Avatar answered Nov 15 '22 20:11

TabLeft


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

like image 9
epascarello Avatar answered Nov 15 '22 20:11

epascarello


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

like image 5
Malik Shahzad Avatar answered Nov 15 '22 21:11

Malik Shahzad