views:

104

answers:

2

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() ?

A: 

use ev.ctrlKey to detect if the ctrl key is pressed (it is a boolean value).

Erik Vold
+2  A: 

In XUL this is done by:

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

see: xul tutorial - keyboar shortcuts

nardi