Unable to dispatch keypress

I am trying to make a firefox add-on using jpm (addon-sdk/jetpack) to translate keystrokes ‘as-you-type’ (i.e., capture events from keyboard, remap certain keys and dispatch the new key events instantly). I am able to capture the keys and block them from appearing on the webpage but unable to generate/dispatch the remapped keys to the webpage. Below is my code,

main.js: code snippet

var pageMod = require("sdk/page-mod");
var data = require("sdk/self").data;
pageMod.PageMod({
    include: "*",
    contentScriptFile: data.url("mykey.js")
});

mykey.js: Full code

window.addEventListener("keypress", captureKeyPress);
function captureKeyPress(ev)
{
    if( ev.cancelable && ev.charCode && !ev.altKey && !ev.ctrlKey){ //  event is cancelable not NULL, CTRL+ nor ALT+
        if((ev.charCode >= 97) && (ev.charCode <= 122)){ // a-z charecters
           ev.preventDefault();
            //ev.stopPropagation ? ev.stopPropagation() : (ev.cancelBubble = true);
            generateKeyPress(String.fromCharCode(ev.charCode-32), ev.originalTarget); // a-z => A-Z  (example)
        }
    }
}

function generateKeyPress(ch, event_object) 
{
    var pressEvent = document.createEvent ("KeyboardEvent");
    pressEvent.initKeyEvent ("keypress", true, true, null, false, false, false, false, 0, ch.charCodeAt(0));
    event_object.dispatchEvent (pressEvent);
}

This code simulates the key and it is also caught by captureKeyPress(), but it is not seen on the webpage (text area). After some debugging, I found that the original key-event which goes into the webpage is “Trusted” and the simulated key-event which is not seen on the webpage is Not “Trusted”. How to overcome the issue and dispatch the simulated key into the webpage ?

The target must be visible on the screen, it must be within the scroll viewport area. I think. Thats how it was in my experience for sending drag and click events.

Yes, the target text-box is in focus but still it does not work.

I posted this question on other forums also (few months back) but did not get the solution yet. I have seen some ‘as-you-type’ translating extensions, but none of them were done using SDK. Can anyone point me to some working SDK add-on that simulates keystrokes ? Is it possible to simulate keys using SDK/jetpack ?

Check out ChromeUtils, i do key dispatching with it you can just extract what you need:

here are examples that work for sure: