Quick note: I do believe that w3c browsers use the keyCode attribute of the event object as well, but only on the keydown/up event. On the press event you should use charCode.
The normal cross-browser method is evt.keyCode||evt.charCode for onKeyPress.
Also, it's generally better to use a cross-browser observe event function (there's a few on here) rather than overriding the document.onX property.
Last, just a stylistic note: you could have defined the members of the ShortcutMan object implicitly within the object definition, rather than explicitly as properties of the object.
Anyway, I've made the changes I'm referring to in the below code.
PS: by the by, I've never heard of document.defaultAction; what's it do?
var ShortcutMan = { //Stylistic: stitched all functions into single object declaration
getEventCode: function(evt){ if(!evt) evt = window.event; //Stylistic: all initial declarations in a single 'var' declaration var
code = evt.charCode||evt.keyCode,
ctrl = evt.ctrlKey ? 'c' : '',
alt = evt.altKey ? 'a' : '',
shift = evt.shiftKey ? 's' : ''; if(code >= 65 && code <= 90)
code+=32; return ctrl + alt + shift + code; }
keyShortcuts: {},
registerShortcut: function(code, action){ if(!/^c?a?s?\d{1,3}$/.test(code)) return;
ShortcutMan.keyShortcuts[code] = action; },
readShortcut: function(evt){ var code = ShortcutMan.getEventCode(evt); //Stylistic: Save a few bytes by predefining the simple 'else' value, // and overriding that later
document.defaultAction = true; //No need for a string comparison in a managed variable if(!!ShortcutMan.keyShortcuts[code]){
ShortcutMan.keyShortcuts[code](); //Slightly more complete events cancelling if(!!evt.stopPropagation) evt.stopPropagation(); else evt.cancelBubble = true; if(!!evt.prerventDefault) evt.preventDefault();
document.defaultAction=evt.returnValue=false; } return document.defaultAction; } } (function(){ /*
Rather the simplest cross-browser event observe operation
Reason: doesn't get in the way of other global shortcut stuff
Reason to function-wrap it: I use the temp variable '_' which
may be used in the global scope
*/ var _=!!document.attachEvent;
document[_?'attachEvent':'addEventListener']((_?'on':'')+'keypress',ShortcutMan.readShortcut); })();
...not because the code is bad, but because it's a good way to do something evil. Repeat after me: "I solemnly swear to never alter the users' environment outside the confines of my page."
I've got good news, and I've got bad news: The universe is merely a figment of my imagination. Now are you ready for the bad news?
The normal cross-browser method is evt.keyCode||evt.charCode for onKeyPress.
Also, it's generally better to use a cross-browser observe event function (there's a few on here) rather than overriding the document.onX property.
Last, just a stylistic note: you could have defined the members of the ShortcutMan object implicitly within the object definition, rather than explicitly as properties of the object.
Anyway, I've made the changes I'm referring to in the below code.
PS: by the by, I've never heard of document.defaultAction; what's it do?
//Stylistic: stitched all functions into single object declaration
getEventCode: function(evt) {
if (!evt) evt = window.event;
//Stylistic: all initial declarations in a single 'var' declaration
var
code = evt.charCode||evt.keyCode,
ctrl = evt.ctrlKey ? 'c' : '',
alt = evt.altKey ? 'a' : '',
shift = evt.shiftKey ? 's' : '';
if (code >= 65 && code <= 90)
code+=32;
return ctrl + alt + shift + code;
}
keyShortcuts: {},
registerShortcut: function(code, action) {
if (!/^c?a?s?\d{1,3}$/.test(code))
return;
ShortcutMan.keyShortcuts[code] = action;
},
readShortcut: function(evt) {
var code = ShortcutMan.getEventCode(evt);
//Stylistic: Save a few bytes by predefining the simple 'else' value,
// and overriding that later
document.defaultAction = true;
//No need for a string comparison in a managed variable
if (!!ShortcutMan.keyShortcuts[code]) {
ShortcutMan.keyShortcuts[code]();
//Slightly more complete events cancelling
if (!!evt.stopPropagation) evt.stopPropagation();
else evt.cancelBubble = true;
if (!!evt.prerventDefault) evt.preventDefault();
document.defaultAction=evt.returnValue=false;
}
return document.defaultAction;
}
}
(function () {
/*
Rather the simplest cross-browser event observe operation
Reason: doesn't get in the way of other global shortcut stuff
Reason to function-wrap it: I use the temp variable '_' which
may be used in the global scope
*/
var _=!!document.attachEvent;
document[_?'attachEvent':'addEventListener']((_?'on':'')+'keypress',ShortcutMan.readShortcut);
})();
I've got good news, and I've got bad news:
The universe is merely a figment of my imagination.
Now are you ready for the bad news?