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()
?
In XUL this is done by:
<keyset>
<key id="key_close" key="W" modifiers="control" oncommand="window.close();" />
</keyset>
see: xul tutorial - keyboar shortcuts
use ev.ctrlKey to detect if the ctrl key is pressed (it is a boolean value).
精彩评论