Execute Ctrl + D on button click

I am trying to simulate keypress events like Ctrl + D when a button is clicked.

It would be great if someone could point me in the right direction, how to achieve the same.

+3
source share
3 answers

You are not allowed to do this. Imagine all the chaos that I could cause if I could send CTRL-ALT-DEL at will.

+1
source

The user event trigger code (in this case, Ctrl + d) is as follows:

var evt = jQuery.Event("keypress");
evt.keyCode = 100; // d
evt.ctrlKey = true;
$(document).trigger(evt);

NB that, as the other answers said, it will be limited in its impact. Thus, you will not be able to affect the normal functions of the browser.

+1

It will be a "shoot", although I leave you an exercise to find the correct code.

As the other guy said, you can't do anything about it. It is purposefully limited.

However, let's say I have a wysiwyg editor in javascript that supports getting ctrl + s and saving, you have to be able to do it yourself and save anyway.

After all, it is a matter of context (focus), and which sometimes fails (again, purposefully).

0
source

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


All Articles