1

I am trying to make a firefox add-on using addon-sdk-1.17 to capture events from keyboard, remap certain keys and dispatch the new key events. 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  
        }
    }
}

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 debuggin, 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 ?

4

0 回答 0