0

我正在尝试设置一个 TamperMonkey 脚本来将密钥重新分配Right ArrowF密钥。

我尝试了这段代码,但到目前为止,当我按下 时没有任何反应F

    (function(){
document.addEventListener('keydown', function(e) {
 // pressed F
  if (e.keyCode == 70 && !e.shiftKey && !e.ctrlKey && !e.altKey && !e.metaKey) {
  keyCode == 39 // this should trigger the right arrow
  }
}, false);
})();

有人可以启发我吗?

4

2 回答 2

1

您需要使用构造KeyboardEvent函数,然后通过,等在整个或特定元素上运行。dispatchEventdocumentdocument.querySelectordocument.getElementById

运行下面的代码片段以查看此操作。

(顺便说一句,usingKeyboardEvent.keyCode不赞成使用KeyboardEvent.key. 它仍然适用于主要的网络浏览器以实现向后兼容性,但它已在标准中被正式弃用)

document-end(如果您想在自定义 DOM 元素上运行键盘事件,还要确保 TamperMonkey 脚本正在运行)

window.addEventListener('keydown', (e) => {
    if (e.key == 'ArrowRight') console.log('right arrow pressed');
});

// Your TamperMonkey script starts here
(() => {
    window.addEventListener('keydown', (e) => {
        if (e.key == 'f' && !e.shiftKey && !e.ctrlKey && !e.altKey && !e.metaKey) {
            console.log('f pressed');

            // https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/KeyboardEvent
            const keyboardEvent = new KeyboardEvent('keydown', {
                key: 'ArrowRight',
                keyCode: 39,
                bubbles: true,
                cancelable: true,
                shiftKey: false,
                ctrlKey: false,
                altKey: false,
                metaKey: false,
            });

            // https://stackoverflow.com/a/44190874/12101554
            // replace `document` with a specific element if you want to do a specific
            document.dispatchEvent(keyboardEvent);
        }
    });
})();
<p>
  press right arrow &amp; f key to see things happen
</p>
<button onclick="document.querySelector('.as-console').innerHTML = ''">clear console</button>

但是,没有办法禁止f在该网站上执行其他操作。如果你想这样做,你可以安装 AutoHotKey (Wikipedia Link),用这一行创建一个 AutoHotKey 脚本:

f::^Right

https://www.autohotkey.com/docs/misc/Remap.htm#Remap

当您启动此 AHK 脚本时,AHK 将在(大多数)程序之前拦截关键事件并对其进行修改。

于 2021-06-18T22:34:34.520 回答
0

正如@samathingamajig 所解决的那样,我刚刚将此脚本设置为键A解释Left Arrow,并D解释Right Arrow

它最终在我的TamperMonkey

// MEDIA TO THE RIGHT - ASSIGNED TO *D* KEY
(() => {
    window.addEventListener('keydown', (e) => {
        if (e.key == 'd' && !e.shiftKey && !e.ctrlKey && !e.altKey && !e.metaKey) {
            ;

            const keyboardEvent = new KeyboardEvent('keydown', {
                key: 'ArrowRight',
                keyCode: 39,
                bubbles: true,
                cancelable: true,
                shiftKey: false,
                ctrlKey: false,
                altKey: false,
                metaKey: false,
            });

            document.dispatchEvent(keyboardEvent);
        }
    });
})();


// MEDIA TO THE LEFT - ASSIGNED TO *A* KEY
(() => {
    window.addEventListener('keydown', (e) => {
        if (e.key == 'a' && !e.shiftKey && !e.ctrlKey && !e.altKey && !e.metaKey) {
            ;

            const keyboardEvent = new KeyboardEvent('keydown', {
                key: 'ArrowLeft',
                keyCode: 39,
                bubbles: true,
                cancelable: true,
                shiftKey: false,
                ctrlKey: false,
                altKey: false,
                metaKey: false,
            });

            document.dispatchEvent(keyboardEvent);
        }
    });
})();
于 2021-06-19T05:05:48.390 回答