2

I am a fresher and working on a Coupons website.

I want to know, when a window loads, how to press ctrl+c itself.

Ctrl+c keys should be pressed automatically right after the loading of window.

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
user7507073
  • 21
  • 1
  • 3
  • 1
    Why not just perform the action that the CTRL+C key should perform with: `document.execCommand("copy")` – Scott Marcus Feb 02 '17 at 17:04
  • @ScottMarcus Not copy but ctrl+c. It just the start, there are lots of other complex things there and only ctrl+c would work there as my Team Leader told me. – user7507073 Feb 02 '17 at 17:06
  • @user7507073 CTRL+C is the operating system command for copy. But, my answer would still be the same. Just call the functions that initiate the "lots of other complex things". The key presses aren't necessary. – Scott Marcus Feb 02 '17 at 17:08
  • @ScottMarcus Alright dear, thank you! But, isn't there any way to do so? Your help will be highly appreciated. – user7507073 Feb 02 '17 at 17:11
  • Possible duplicate of [How to simulate keypress using Javascript?](http://stackoverflow.com/questions/29288247/how-to-simulate-keypress-using-javascript) – Teemu Feb 02 '17 at 17:20
  • @l.hussain I chose it because of mccainz'es answer. There's a dup link at the top of the question to the similar code Zakaria has provided in their answer. Note, that the code in the that answer triggers an event emulating keypresses only, it doesn't really "press" the keys on the keyboard. – Teemu Feb 02 '17 at 17:58
  • @Teemu The question in your suggested dup link is 1- about simulating the `keypress` and not a `keypress` combination. 2- mccainz's answer hasn't provided any code example (what considered really a bad answer in this case) and i think that why it has no upvotes. anyway thanks for your intervention. – Zakaria Acharki Feb 02 '17 at 18:06
  • @ZakariaAcharki ?? When they say "you can't" how good idea it would be to provide some code? The linked dup in that question contains quite similar answers to yours, though. – Teemu Feb 02 '17 at 18:08

1 Answers1

8

Working fiddle.

Since you're using jQuery you could do it using jQuery.Event :

var e = jQuery.Event("keydown");

e.which = 67; // 'C' key code value
e.ctrlKey = true;

$("body").trigger(e);

Hope this helps.

jQuery Solution

$(function(){ //Ready function
  
  //Event that catch the Ctrl+C press (Just for test)
  $("body").keydown(function(e) {
    var code = (e.keyCode ? e.keyCode : e.which);

    if (code == 67) {
      if (e.ctrlKey) {
        alert("Ctrl+C was pressed!!");
      }
    }
  });

  //Event that trigger the 'Ctrl+C' 
  var e = jQuery.Event("keydown");
  
  e.which = 67; // 'C' key code value
  e.ctrlKey = true;
  
  $("body").trigger(e);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Javascript Solution

document.addEventListener('keydown', function (e) {
  if (event.altKey && event.key === 'n') 
  {
    document.querySelectorAll('._180aQody._254QuS8h')[0].click();
  }
});

document.querySelector('._180aQody._254QuS8h').addEventListener('click', function (e) {
  alert("Alt+n was pressed!!");
});

var ev = new KeyboardEvent('keydown', {bubbles: true,  altKey: true,  key: "n"});
document.querySelectorAll('._180aQody._254QuS8h')[0].dispatchEvent(ev);
<button class='_180aQody _254QuS8h'>CLick me using Alt+n</button>
Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
  • I would love to do so, I don't know how implement that on a page when a page loads. May you please change it to ctrl+a with live onload example. Content selected when page loads. For example user click on a link, new windows opens and after a while whole content get selected by ctrl+a pressed using jquery. – user7507073 Feb 02 '17 at 17:20
  • 1
    If you want to select the page content your answer is [HERE](http://stackoverflow.com/questions/985272/jquery-selecting-text-in-an-element-akin-to-highlighting-with-your-mouse/987376#987376) – Zakaria Acharki Feb 02 '17 at 17:27
  • But how to do it using ctrl+a on page load, please last time explain, I will be highly thank full of you. I want to learn how to press keys using jquery. – user7507073 Feb 02 '17 at 17:33
  • 1
    Glad i could helps. You've just to change the `67` number in my code by the decimal number of the character you want (find the list [HERE](http://ascii.cl/)) you could see that '67'=>'C'. – Zakaria Acharki Feb 02 '17 at 17:37