2

Possible Duplicate:
How to detect right mouse click + paste using JavaScript?

How to capture event when anyone paste after right click and then paste? I want to alert when any one paste anything in textbox after right click and paste. In javascript is there any way to know right click and paste?

Community
  • 1
  • 1
Rohan Kumar
  • 38,998
  • 11
  • 69
  • 99

6 Answers6

3
document.onmousedown=doSomething;  



function doSomething(e) {
        var rightclick;
        if (!e) var e = window.event;
        if (e.which) rightclick = (e.which == 3);
        else if (e.button) rightclick = (e.button == 2);
        alert('Rightclick: ' + rightclick); // true or false
    }

which is an old Netscape property. Left button gives a value of 1, middle button (mouse wheel) gives 2, right button gives 3. W3C and Microsoft happen to agree on this one and give right button a value of 2. The combination of e.which and e.button is used as in Mozilla e.button , for ctrl-click the value is 2.

Anand
  • 13,140
  • 6
  • 29
  • 43
2
$("#pasteable").on('paste',function(event){
    setTimeout(function(){
        alert($("#pasteable").val());
    },100);
});

fiddle : http://jsfiddle.net/BgW7x/

dku.rajkumar
  • 17,470
  • 7
  • 39
  • 57
2
var elem = document.getElementById("myID");

elem.onclick = function( event ){
    var event = event || window.event;
    if( event.which === '3' ){
        // it was a right click
    }
};

There's your pure Javascript way to detect whether an element has been clicked with the right mouse button. Like other people have noted, you can use jQuery to detect the paste event. Here is an answer that explains how to detect a paste event without jQuery.

Community
  • 1
  • 1
Purag
  • 16,273
  • 4
  • 48
  • 70
0

Capture a normal click event (e.g. with jQuery: $( node ).click( function( ev ){ ... } ) and check if the which property >= 2:

$( node ).click( function( ev ){
    if( ev.which >= 2 ){
        // right click
    }
} );

Note that the exact value of which to detect right-click changes based on browser (but I think it's always either 2 or 3). See http://www.quirksmode.org/js/events_properties.html for more details.

Mark Kahn
  • 81,115
  • 25
  • 161
  • 212
0

Have a look at this

http://jsfiddle.net/uUqrF/enter link description here

keydown event is added to handle specific scenario,

e.g. If u right clicked on text box and then try to paste from ctrl+v

In this case paste event detect this paste also.

jaychapani
  • 1,329
  • 1
  • 10
  • 27
-1

check for the mouse button in the click event by using the event property

$('#element').click(function(event) {
    if  (event.which==3) {
            alert('Right mouse button pressed');
            break;
    }
});

for more options read this

for using jquery you have to include the jquery library in your head tag

<script type="text/javascript" src="jquery library path which you can download from jquery site or use google jquery library"></script>

the you have to use another <script> tag for writing you jquery

<script type="text/javascript"></script>
$(document).ready(function(){

$('#element').click(function(event) {
        if  (event.which==3) {
                alert('Right mouse button pressed');
                break;
        }
    });

});
<script type="text/javascript">

</script>

for more detail read jquery documentation, do a little google

Community
  • 1
  • 1
Dau
  • 7,478
  • 4
  • 21
  • 44
  • The OP didn't ask how to do it with jQuery. – Purag Dec 30 '11 at 09:32
  • Last I checked, jQuery was also Javascript. Has that changed, @Purag ? – conradkleinespel Aug 17 '14 at 18:20
  • @conradk Haha, you dug up a pretty old comment. I think the reason I added my previous comment was because the OP may not have been including jQuery on the page in question, which would require a vanilla JS solution. It would have been useful to have both. :) – Purag Aug 22 '14 at 10:10