1

how can I simulate (trigger) multiple keypress upon press of a button using javascript (javascript and html), while having the window being the event target.

I basically wish to create a button, when it's clicked, it will simulate (trigger) ctrl+w

I was trying to mix and match and try lots of different codes around this subject, but couldn't get the multiple key press to work properly. In addition, because I am new to javascript I don't yet fully understand how to turn all of this into a function that can be called upon press of a button.

Have a nice day, looking forward to the answers.

  • 1
    I think you want this: https://stackoverflow.com/questions/596481/is-it-possible-to-simulate-key-press-events-programmatically – Blunt Jackson Apr 30 '22 at 22:36
  • Are you sure you need to simulate the key presses to trigger your effect? I know that by default in most browsers, control-W closes the current tab. Do you ultimately want to close the tab when your button is clicked, and this is your attempt to do so? – Kevin Apr 30 '22 at 22:54
  • If you're trying to close the current tab, unfortunately, it's very difficult to do. Modern browsers deliberately do not allow tabs to be closed by JavaScript unless the tab was expressly created via JavaScript. This means that closing a tab requires some very nasty workarounds unless you used `window.open()` to seent the user to the tab in the first place. See https://developer.mozilla.org/en-US/docs/Web/API/Window/close – Kevin Apr 30 '22 at 23:00
  • thank you very much for your helpful answers !!! i know the self.close() but i rather wish to create a set of buttons that can not only close individual tabs but can also minimize windows and change window sizes, or trigger different shortcuts such as find functions. i just wish to find out if these types of shortcuts can be triggered via javascript, to see if they can be used to create macros or to kind of remote-control different basic functions of a browser. if javascript is limited in achieving this i'm also flexible to trying c, hta, or vbscript and so forth – skyblue_bluesky Apr 30 '22 at 23:13
  • would it also be difficult to have javascript to open a new tab? via ctrl+t ? – skyblue_bluesky Apr 30 '22 at 23:14
  • Gotcha, that makes sense! I think you'll find more luck trying to make all of this work directly from JavaScript instead of trying to trigger shortcuts. Some of the things you asked about can be completed in JavaScript just fine! Use [`window.open()`](https://developer.mozilla.org/en-US/docs/Web/API/Window/open) to open a website in a new tab (or window is that's how the user set their browser preferences). Use [`window.resizeTo()`](https://developer.mozilla.org/en-US/docs/Web/API/Window/resizeTo) to change the size of the window. – Kevin Apr 30 '22 at 23:21
  • Other things you've asked about, unfortunately, are not possible within JavaScript itself. This is surprising, but there are very serious security reasons for it. A lot of it boils down to making it difficult for scammers to trick the user into doing something they don't want to. For example, allowing tabs to be closed could allow a bad actor to open two tabs at once, then close one right as the user clicks where they want them to, tricking the user into clicking a link on the second page that does something malicious. – Kevin Apr 30 '22 at 23:35
  • If your goal is to create a tool that can manipulate browser windows for the user, I think you're much more likely to find success if you write an app that lives outside of the browser instead of trying to get it to work inside the browser. Unfortunately, I don't have good advice for you on how to start with this, but I think you'll find it requires far fewer workarounds and allows you to do more once you've gotten the groundwork for your project set up. – Kevin Apr 30 '22 at 23:37

0 Answers0