vendredi 13 février 2015

XULrunner Key Element Listener That Will Run The Menuitems Command Event


In XULrunner I am trying to add keyboard shortcuts (Ctrl+N) to my menus. I would like to just have the <key> run each particular menuitems command function. The entire keyset is generated in PHP, so xul and/or javascript has to figure out what exact function needs to be fired.


The menuitems look something like this



<menuitem id="refresh-menu" label="Refresh" funcname="RefreshWin" key="refresh-key" />


I have setup the command event listeners for the menu like so



menubar.addEventListener('command', MenuLauncher, false);

function MenuLauncher(e)
{
if (e.target !== e.currentTarget && e.target.getAttribute('disabled') != 'true')
{
if (e.target.nodeName == 'menuitem')
{
var funcname = e.target.getAttribute('funcname');

if (typeof WindowControls[funcname] != 'undefined')
{
WindowControls[funcname]();
}
}
}
}
var WindowControls =
{
RefreshWin: function()
{
window.location.reload();
},

//other functions
};


And my keys look something like such



<key id="refresh-key" modifiers="control" key="R" />


The MenuLauncher looks at the e.target object and grabs the custom attribute funcname and I use that to call the correct function.


If the key could just fire a click() on the menuitem object it would work. Or if it could just pass the menuitems object to the MenuLauncher, something like oncommand="MenuLauncher([menuitemobject])".


All efforts to find the trick to this have failed right now it looks like it would be easier to just use a pure javascript keyboard event listener.





Aucun commentaire:

Enregistrer un commentaire