简体   繁体   中英

Is it possible to simulate a key combination press in browser with Javascript?

I need to open a new tab in browser and focus back on the previous one with the key combination: Crtl + Tab + Shift . I read most of the solutions here and they are deprecated from JS now. If there is any other approach, please share.

I don't think you can simulate this because it would be highly safety problem that a browser can enter key events for you.

Just imagine the browser would enter Crtl + Shift + Esc and your task manager would open and stuff like that.

The question is what exactly is your goal? Maybe there is just another way to reach it.

If you want to stay in your browser tab and just show other content to the user on click or something you could do it with an iframe or just show the content in a fancybox or something like that.

Key events are possible with javascript.

So I think you could say

document.addEventListener('keydown', function(event) {
  if (event.code == 'ControlLeft' && 'ctrlKey' && 'Tab') {
    window.open('http://www.eeedddö.de','_blank');
  }
});

Problem is the Strg + Tab key just switches your Browser Tab so I think you need an other key kombination without Strg + Tab key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM