所以我在做什麼時,在某個網站上按下鍵「e」,然後多次按下鍵「w」。我做了測試,看腳本是否達到了用alert()按下「e」的地步。它的工作,但它不會觸發關鍵「W」。當按下一個鍵時,多次按下不同的按鍵?
// @run-at document-end
// ==/UserScript==
(function() {
var amount = 6;
var duration = 50; //ms
var overwriting = function(evt) {
if (evt.keyCode === 69) { // KEY_E
for (var i = 0; i < amount; ++i) {
setTimeout(function() {
alert("Key e is pressed"); /* This works */
window.onkeydown({keyCode: 87}); // KEY_W /* This doesn't */
window.onkeyup({keyCode: 87});
}, i * duration);
}
}
};
window.addEventListener('keydown', overwriting);
})();
的可能的複製[如何在JavaScript觸發事件?(http://stackoverflow.com/questions/2490825/how-to-trigger-event-in-javascript) – Eduardo