问题
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
回答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.
来源:https://stackoverflow.com/questions/10366896/have-a-script-simulate-a-control-keypress-sequence