Dispatching keyboard event

I’m trying to dispatch a keyboard event via. a content script. This is what I’ve while experimenting:

var prev;
var triggering;

document.addEventListener("keydown", function(ev) {
  if (prev && !triggering) {
    var k = new KeyboardEvent("keydown", prev);
    triggering = true;
    document.dispatchEvent(k);
    triggering = false;
  }

  prev = ev;
});

Basically, I catch a key down, and dispatch an event of the same type again (so that all properties are set as expected). If it were to work, I’d have observed every event twice, but that doesn’t happen.

Is this the right way to dispatch a keyboard event?

You’re missing the sharing bit for the event object (k): https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Content_scripts#Sharing_objects_with_page_scripts

You’ll probably also want to cancel the original event.