1

I want to execute the command ctrl + 0 on page load. Here is what I have so far:

$( document ).ready(function() {
    var press = jQuery.Event("keypress");
    press.ctrlKey = true;
    press.which = 48;
    trigger(press);
});

This is not firing ctrl+0 and is also breaking another div on the page.

What am I doing wrong and how do I fix it?

David Tunnell
  • 7,252
  • 20
  • 66
  • 124

2 Answers2

1

You should use $(document).trigger(press) which will track keydowns on the whole document.

$(document).keydown(function(e) {
  if (e.keyCode == 48 && e.ctrlKey)
    $("body").append("<p>ctrl+0 Detected!</p>");
});


$(document).ready(function() {
  var e = jQuery.Event("keydown");
  e.keyCode = 48; // # Some key code value
  e.ctrlKey = true;
  $(document).trigger(e);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
void
  • 36,090
  • 8
  • 62
  • 107
  • This appears to be working, however the browser is not receiving the command. Pressing ctrl+0 should set it's zoom to 100% and it's not happening. – David Tunnell Dec 22 '15 at 17:33
  • Thats an altogether different thing, you can't interact with the Browsing Software. – void Dec 22 '15 at 17:43
  • Probably there is no good browser compatibility, but you have the css `zoom` property which allows you to do what you are looking for. https://developer.mozilla.org/en-US/docs/Web/CSS/@viewport/zoom – kosmos Dec 22 '15 at 22:11
0

You can not trigger ctrl+0 in brownser, its security question. but you can get zoom ratio of brownser.

Device Pixel Aspect Ratio http://tombigel.github.io/detect-zoom/

github of project https://github.com/tombigel/detect-zoom

Maybe is useful in you project.

RBoschini
  • 496
  • 5
  • 16