我正在尝试使用addon-sdk-1.17创建一个firefox附加组件,以便从键盘捕获事件,重新映射某些键并调度新的键事件。我能够捕获密钥并阻止它们出现在网页上,但无法生成/发送重新映射的密钥到网页。以下是我的代码,
main.js :代码段
var pageMod = require("sdk/page-mod");
var data = require("sdk/self").data;
pageMod.PageMod({
include: "*",
contentScriptFile: data.url("mykey.js")
});
mykey.js :完整代码
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);
}
此代码模拟密钥,它也被captureKeyPress()捕获,但在网页(文本区域)中看不到。经过一些调试后,我发现进入网页的原始密钥事件是&#34; Trusted&#34;并且在网页上看不到的模拟键事件不是&#34; Trusted&#34;。如何克服这个问题并将模拟密钥发送到网页?