4

I've searched and discovered that I can simulate a keypress event using jQuery, but my knowledge about jQuery is poor and I didn't get how exactly.

So I have a Greasemonkey script which manages 2 different webpages in 2 tabs.
I'd like to simulate/auto-execute CtrlShiftTab to go back to the previous tab automatically, this way I could change tabs in Firefox.

The problem is that it's not only a plain keypress, I need to simulate a Ctrl and Shift parts too.

I've added this to my script:

// @require        http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js

and tried:

function testEvents(){
var press = jQuery.Event("keypress");
press.which = 9;
$("whatever").trigger(press.ctrlKey + press.shiftKey + press.which);

}

without success.

Some links I've seen: Is it possible to simulate key press events programmatically?, simulate jquery, and http://forum.jquery.com/topic/simulating-keypress-events

Community
  • 1
  • 1
Commentator
  • 640
  • 1
  • 6
  • 22

1 Answers1

1

JavaScript cannot be used to trigger a default behavior by simulating "shortcut" key events.

Since jQuery is just a different way to write JavaScript code, the restriction also applies.
And, because Greasemonkey scripts are based on JavaScript, without extra functionality besides the GM_* methods, it is not possible to write a User script which switches tabs.

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • So which `GM_*` method allows this? – Tom Jan 04 '14 at 14:30
  • @Tom None. Read the sentence as "gm scripts have no extra functionality besides `GM_*` methods", "so it is not possible to write a user script to switch tabs". – Rob W Jan 04 '14 at 17:41
  • While is true that javascript cannot use tabs, is not impossible to do that. – m3nda Jan 24 '15 at 02:33
  • @erm3nda Are you aware of a way to switch tabs without showing a modal dialog in Greasemonkey? If so, post it as an answer, others may be interested in your findings. – Rob W Jan 24 '15 at 09:09
  • It would be not easy to explain. Lot of things involved for a general answer. If i see a piece of code or the operation's flow i can put an answer. I need to know why is really need to change tab, because the javascript runs also on a hided tab. So... need more explanation about the problem. – m3nda Jan 24 '15 at 12:31