Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger Ctrl+C key event with jQuery?

I want to simulate Ctrl+C to copy the text from a page. I first tried this:

$('#codetext').click( function() {
  $("#codetext").trigger({
    type:  'keydown',
    which:  99
  });
}

HTML:

<input type='text' id='codetext'>

I have also tried using $(this) instead of the selector, but the input element also has focus on it, so it doesn't run.

like image 682
Angelus Avatar asked Oct 08 '10 12:10

Angelus


People also ask

How to get the key pressed in jQuery?

To check whether user pressed ENTER key on webpage or on any input element, you can bind keypress or keydown event to that element or document object itself. Then in bind() function check the keycode of pressed key whether it's value is 13 is not.

How to detect Ctrl enter in JavaScript?

To detect the combination of keys with “Ctrl”, we use the ctrl property of the keydown event. It returns a “boolean” value to tell if “ctrl” is pressed or not when the key event got triggered.

What is keypress event in Javascript?

The keypress event is fired when a key that produces a character value is pressed down. Examples of keys that produce a character value are alphabetic, numeric, and punctuation keys. Examples of keys that don't produce a character value are modifier keys such as Alt , Shift , Ctrl , or Meta .


2 Answers

Check out ZeroClipboard... I think it works, but I haven't tested it.

like image 71
Mottie Avatar answered Sep 19 '22 00:09

Mottie


not sure how to trigger ctrl+c, but there's a JQuery clipboard plugin that may be of some use:

http://plugins.jquery.com/project/copy

$("#elmID").copy() // copy all text inside #elmID.

like image 32
Ross Avatar answered Sep 19 '22 00:09

Ross