1
<html>
<script>
$('#pasteButton').click( function() {

  // place code for this button
  // ctrl + v shortcut should work for this button
  // how?
  }

});

</script>
<body>
    <input type="button" id="pasteButton" value="Paste" />
</body>
</html>

Thanks in advance. Similarly, I also need to cut / copy selected text from a textarea using buttons.

update from comment section : I have a text area where I select some text and if I click on pasteButton that selected text need to be pasted in my text area

arjuncc
  • 3,057
  • 4
  • 38
  • 71
mbsr
  • 11
  • 1
  • 3

3 Answers3

5

First you have to listen to the keydown event. If it is control+v then you can trigger your function You can use the following code for that purpose:

HTML

<input type="button" name="pasteButton" value="press me" id="pasteButton"/>

JAVASCRIPT

  $(window).keydown(function(event) {
          if(event.ctrlKey && (event.which == 86 || event.which==118)) { 
              $('#pasteButton').trigger("click")
            event.preventDefault(); 
          }
        });
        $('#pasteButton').click( function(){
            alert("hello");
        });

SAMPLE JSFIDDLE

arjuncc
  • 3,057
  • 4
  • 38
  • 71
  • am sorry i think my question is not clear. i want the same functionality of 'ctrl+v' on click of 'pasteButton' – mbsr Apr 10 '13 at 06:07
  • @mbsr, If like that, what is your real requirement? What are you trying to achieve? – arjuncc Apr 10 '13 at 06:08
  • i have a text area where i select some text and if i click on pasteButton that selected text need to be pasted in my text area. – mbsr Apr 10 '13 at 06:14
  • @mbsr, sorry for the late reply, so you need only copy functionality? Or you need 'ctrl+v' short cut too? – arjuncc Apr 10 '13 at 06:38
  • @mbsr, its little more complicated than you expect, refer these similar SO question http://stackoverflow.com/questions/717224/how-to-get-selected-text-in-textarea http://stackoverflow.com/questions/275761/how-to-get-selected-text-from-textbox-control-with-javascript – arjuncc Apr 10 '13 at 06:49
  • Thanks for reply, i have seen these examples, 2nd one 1 used for selecting text but that selected text need to be pasted on click of button. – mbsr Apr 10 '13 at 10:04
  • something like in gre exam while writing awa section we can find this. – mbsr Apr 10 '13 at 10:05
  • how if you only use 1 shorcut key like f7 only. – kim de castro Jan 04 '16 at 02:41
1

Taken from this post:

How to detect Ctrl+V, Ctrl+C using JavaScript?

$(document).ready(function()
{

    $('#pasteButton').click( function()
    {
        // Your click functionality here
    }

    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;
    });

    $(document).keydown(function(e)
    {
        if (ctrlDown && e.keyCode == vKey) {
           // ctrl + V clicked, click the paste button.

           $('#pasteButton').click();
           return false;
        };
    });
});
Community
  • 1
  • 1
Chris Dixon
  • 9,024
  • 5
  • 32
  • 66
0

Just use accesskey attribute of html tag for more detail see following link

http://www.w3schools.com/tags/att_global_accesskey.asp