Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Apr 2007
    Posts
    179
    Thanks
    17
    Thanked 0 Times in 0 Posts

    How to detect right mouse click --> paste

    In a <textarea ..., the user can do this:

    1) copy some text from somewhere;

    2) right-mouse click on the textarea, from the floating menu, select the Paste, the text is pasted in the textarea.

    How can I detect the action of the Paste menuitem being clicked (on IE and Firefox, respectively)?


    Thanks

    Scott

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by otnj2ee View Post

    How can I detect the action of the Paste menuitem being clicked (on IE and Firefox, respectively)?
    I don't think you can.

  • #3
    Regular Coder mjlorbet's Avatar
    Join Date
    Jan 2008
    Location
    Milwaukee, WI
    Posts
    724
    Thanks
    8
    Thanked 96 Times in 95 Posts
    it's not possible, it's a function of the browser, not available to the code it's rendering.

    side note... why are you creating multiple threads on exactly the same thing running at the same time? three to be precise. read the forum rules
    -Mike
    "Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender

  • #4
    Regular Coder
    Join Date
    Mar 2006
    Posts
    725
    Thanks
    35
    Thanked 132 Times in 123 Posts
    What are you trying to prevent?

    It's a losing game to try to block the user from doing anything he wants, but you can keep the value in the textarea from adding more than one character on every keyup,
    and return it to its previous value on any additions to the value that did not result from a key press.
    You will have to store the value somwhere as it is typed into the textarea, and watch all the key,click,focus and blur events, but it is possible.

    The user could still use the keyboard to paste one letter at a time, but it would prevent someone from pasting an answer to a exam question, which is the only use I can think of for this behavior.
    Last edited by mrhoo; 04-18-2008 at 03:03 PM.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    The following detects a paste operation involving a significant block of text (say 50 characters or more) by comparing the length of the text in the text area with the length 5 seconds previously, and raising an alert and erasing the textarea if the length (number of characters) has gone up by more than 50 (more than could be typed in that time).

    The obvious problem is that it does not work if JavaScript is disabled.

    Code:
    <textarea name = "txt" id = "txt" rows = "20" cols = "75"></textarea>

    <script type = "text/javascript">

    var txtlngth = 0;
    function check4paste() {
    var newlen = document.getElementById("txt").value.length
    if (newlen > (txtlngth + 50)) {
    alert ("You appear to have pasted a substantial amount of text into the textarea. This is not allowed!")
    document.getElementById("txt").value = "";
    txtlngth = 0;
    }
    else {txtlngth = newlen}

    var tim = window.setTimeout("check4paste()", 5000); // check every 5 seconds
    }

    check4paste();

    </script>

  • #6
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    3
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by otnj2ee View Post
    In a <textarea ..., the user can do this:

    1) copy some text from somewhere;

    2) right-mouse click on the textarea, from the floating menu, select the Paste, the text is pasted in the textarea.

    How can I detect the action of the Paste menuitem being clicked (on IE and Firefox, respectively)?


    Thanks

    Scott
    It is possible with ie(8 tested) but with firefox i can not find a resonable method, maybe just by checking at interval given...

    With internet explorer8 it can be detected with onbeforeeditfocus or onpropertychange, when you click paste the detectpastemoment is executed on both functions:

    Code:
    <html>
    	<body>
    		<input id="one">
    		<input id="two" onbeforeeditfocus="detectpastemoment()" onpropertychange="detectpastemoment()">
    		<script>
    			j=0;
    			function detectpastemoment(){
    				j++;
    				document.getElementById('one').value=j
    			}
    		</script>
    	</body>
    </html>
    These functions do not return right with firefox and i dont find how to do in firefox.

  • #7
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    3
    Thanks
    0
    Thanked 1 Time in 1 Post
    And for textarea is the same thing.

  • #8
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    3
    Thanks
    0
    Thanked 1 Time in 1 Post
    Right now onpaste its working both firefox and ie8

    Code:
    <html>
    	<body>
    		<input id="one">
    		<input id="two" onpaste="detectpastemoment()">
    		<script>
    			j=0;
    			function detectpastemoment(){
    				j++;
    				document.getElementById('one').value=j
    			}
    		</script>
    	</body>
    </html>

  • Users who have thanked atcoding for this post:

    Philip M (09-17-2009)

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by atcoding View Post
    Right now onpaste its working both firefox and ie8

    Code:
    <html>
    	<body>
    		<input id="one">
    		<input id="two" onpaste="detectpastemoment()">
    		<script>
    			j=0;
    			function detectpastemoment(){
    				j++;
    				document.getElementById('one').value=j
    			}
    		</script>
    	</body>
    </html>

    Interesting - works in IE6 and IE7 and Firefox, but not of course if Javascript is disabled.

    Slight improvement:-


    Code:
    <input id="one" size = "40" value = "Something"><br>
    <input id="two" size = "40" onpaste="detectPaste()">
    
    <script type = "text/javascript">
    
    function detectPaste(){
    document.getElementById('one').value="Naughty boy!";  
    alert ("You have pasted text into this document");
    setTimeout("erase()",10);
    }
    
    function erase(){
    document.getElementById('two').value="";  
    }
    
    </script>
    Last edited by Philip M; 09-17-2009 at 08:20 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •