Search code examples
javascriptfirefox-addondom-eventsxul

How to attach window.close to cntrl+w keypress event in a xul window?


I have a XUL window, and I want the cntrl+w hotkey to close the window, but when I attach:

window.addEventListener("keypress", function(ev) {
  GM_log("onkeypress handler: \n"
  + "keyCode property: " + ev.keyCode + "\n"
  + "which property: " + ev.which + "\n"
  + "charCode property: " + ev.charCode + "\n"
  + "Character Key Pressed: "
  + String.fromCharCode(ev.charCode) + "\n");
}, true);

to the page, it treats pressing 'w' and 'cntrl+w' the same, charCode 119.. how can I determine that cntrl+w was pressed so that I may window.close()?


Solution

  • In XUL this is done by:

    <keyset>
       <key id="key_close" key="W" modifiers="control" oncommand="window.close();" />
    </keyset>
    

    see: xul tutorial - keyboar shortcuts