How to run key combo with jQuery

I encoded some things:

http://fincha.com/kunden/schmitt/

I zoom with .css("zoom") but I need buttons to simulate CTRL + or CTRL -

This code does not work for me:

 e = jQuery.Event("keydown"); e.which = 50; $("input").trigger(e); 

Please help!

EDIT

I actually wanted the zoom-in or zoom-out entire web page, not just the input fields.

+5
source share
2 answers

jQuery normalizes event modifier keys by setting one or more properties of the event object. So you want to set event.ctrlKey to true , so this should work for you:

 e = jQuery.Event("keydown"); e.which = 50; e.ctrlKey = true; $("input").trigger(e); 

However, according to the comment in the source (see below):

You cannot easily change values ​​in an event object (possibly for security reasons).

So, if you cannot set the event properties after creating the event object, you can $.extend() set its ctrlKey property:

 e = jQuery.Event("keydown"); fake = $.extend({}, e, {which: 50, ctrlKey: true}); $("input").trigger(fake); 

One more thing: I'm not sure if you are trying to use key code 50 for the + or - keys. Maybe you are using a different keyboard layout, but according to this demo , 50 is the JavaScript key code for pressing 2 - so this could also be part of your problem.


Source: comments on jQuery API page .


Edit:

All this aside, I don’t think you can really change the zoom level of the browser using JavaScript, even if you β€œsend” a keyboard command for this.

Access browser page zoom controls using javascript

+10
source

Source: http://www.scottklarr.com/topic/126/how-to-create-ctrl-key-shortcuts-in-javascript/

 var isCtrl = false; $(document).keyup(function (e) { if(e.which == 17) isCtrl=false; }).keydown(function (e) { if(e.which == 17) isCtrl=true; if(e.which == 83 && isCtrl == true) { //run code for CTRL+S -- ie, save! return false; } }); 

This is for Ctrl + s, but you should easily modify it.

+1
source

Source: https://habr.com/ru/post/1238972/


All Articles