Simulate key press in a plugin

I’m trying to emulate a key press (“r” for example) inside a plugin.
I was testing with:

import PluginBase from ‘chrome-extension://lnnmjmalakahagblkkcnjkoaihlfglon/dist/modules/plugin-base.js’;
import ExtensionUtil from ‘chrome-extension://lnnmjmalakahagblkkcnjkoaihlfglon/dist/modules/extension-util.js’;

function backendPressKey(codes){const codesArr=[].concat(codes);chrome.runtime.sendMessage({type:“pressKeys”,payload:{codes:codesArr,nonChar:!0}});}

function pressKey(key,code=0){return backendPressKey(code),!0}

var …

{name:“SC41”,description:“bla bla bla”,match:“hi”,fn:()=>{alert(“in”);pressKey(0,76);alert(“end”)}}

but it doesn’t work.

Thank you in advance for your help!!


Unfortunately, pressing a key is not straightforward in the Browser. Can you describe your end-goal more so that I can help you find an ideal solution?

Also, can you be more detailed with “it doesn’t work”. Are there any errors in the console? Do the alerts popup? What pages have you tried it on (certain pages won’t work due to a Chrome bug - eg. pages with associated Chrome apps, but there is a workaround).

FYI you can use triple backquotes to make your code more readable here.