5

I am trying to capture the text on Ctrl+V event as below..

  1. Creating a textarea in the page and setting height 0px and width 0px. like below

      <textarea id="a" style="height:0px;width:0px"></textarea>
    
  2. On pressing V key i am setting the focus to that textarea and then using Ctrl+V button. Like below..

     shortcut.add("X",function() {
      $('#a').focus();
     });
     // In between user have to press Ctrl+V to paste the content
     shortcut.add("V",function() {
      alert($('#a').val());
     });
    

I think this as a most inefficient approach and waiting for valuable suggestions to improve this..

Mwiza
  • 7,780
  • 3
  • 46
  • 42
Exception
  • 8,111
  • 22
  • 85
  • 136
  • It's called google. http://stackoverflow.com/questions/237254/how-do-you-handle-oncut-oncopy-and-onpaste-in-jquery and http://www.quirksmode.org/dom/events/tests/cutcopypaste.html –  Nov 27 '11 at 12:58
  • 1
    There's no real alternative to this, I'm afraid. – Tim Down Nov 27 '11 at 19:28
  • 1
    @Consciousness: I don't think you've read the question properly: the OP wants to capture the text pasted, which is not generally possible using the paste event. – Tim Down Nov 27 '11 at 19:29

2 Answers2

2

You can attach events to the paste event.

$('textarea').bind('paste', function() {
   // Hello, Mr. Paste!
});
alex
  • 479,566
  • 201
  • 878
  • 984
  • On paste event I cannot capture the pasted text.. Like $(this).val() returns me null/previously pasted text. – Exception Nov 27 '11 at 13:10
  • You could update the string into a variable every time it's changed, and if it's a paste event compare the strings –  Nov 27 '11 at 20:38
0

You can capture CTRL + V as :

$(document).ready(function()
{
    var ctrlDown = false;
    var ctrlKey = 17, vKey = 86;

    $(document).keydown(function(e)
    {
        if (e.keyCode == ctrlKey) ctrlDown = true;
    }).keyup(function(e)
    {
        if (e.keyCode == ctrlKey) ctrlDown = false;
    });

    $("textarea").keydown(function(e)
    {
        if (ctrlDown && (e.keyCode == vKey)) return false;
    });
});

Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162