Last active
March 21, 2016 11:05
-
-
Save raphaeladrien/db1a3f9a7bcd457aa222 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//TO CALL hotkeys( ALT, '1', function(){ alert('Hello Nurse!!!'); } ); | |
const CTRL = 'ctrl+'; | |
const ALT = 'alt+'; | |
const CTRL_ALT = 'ctrl+alt+'; | |
(function (window) { | |
'use strict'; | |
const _hotkeys = {}; | |
function hotkeys( modifyKeys, character, method ){ | |
let key = modifyKeys + character; | |
_hotkeys[key] = method; | |
} | |
addEvent(document, 'keydown', function(event) { | |
dispatch(event); | |
}); | |
function getKey( event ){ | |
let key; | |
const specialKeys = { | |
8: "backspace", 9: "tab", 13: "return", 16: "shift", 17: "ctrl", 18: "alt", 19: "pause", | |
20: "capslock", 27: "esc", 32: "space", 33: "pageup", 34: "pagedown", 35: "end", 36: "home", | |
37: "left", 38: "up", 39: "right", 40: "down", 45: "insert", 46: "del", | |
96: "0", 97: "1", 98: "2", 99: "3", 100: "4", 101: "5", 102: "6", 103: "7", | |
104: "8", 105: "9", 106: "*", 107: "+", 109: "-", 110: ".", 111 : "/", | |
112: "f1", 113: "f2", 114: "f3", 115: "f4", 116: "f5", 117: "f6", 118: "f7", 119: "f8", | |
120: "f9", 121: "f10", 122: "f11", 123: "f12", 144: "numlock", 145: "scroll", 188: ",", 190: ".", | |
191: "/", 224: "meta" }; | |
const shiftNums = { | |
"`": "~", "1": "!", "2": "@", "3": "#", "4": "$", "5": "%", "6": "^", "7": "&", | |
"8": "*", "9": "(", "0": ")", "-": "_", "=": "+", ";": ": ", "'": "\"", ",": "<", | |
".": ">", "/": "?", "\\": "|" | |
}; | |
if( event.shiftKey ){ | |
key = shiftNums[ String.fromCharCode( event.which ).toLowerCase() ]; | |
} | |
else { | |
key = specialKeys[ event.which ] === undefined | |
? String.fromCharCode( event.which ).toLowerCase() | |
: specialKeys[ event.which ]; | |
} | |
return key; | |
} | |
function dispatch( event ){ | |
try { | |
let key = getKey( event ); | |
const modif = []; | |
if ( event.ctrlKey ) { | |
modif.push( CTRL ); | |
} | |
if ( event.altKey ) { | |
modif.push( ALT ); | |
} | |
key = modif.join('') + key; | |
var method = _hotkeys[key] | |
if(typeof method === 'function'){ | |
method.call( null ); | |
} | |
} catch( error ){ | |
console.error( error ); | |
} | |
} | |
function addEvent(object, event, method) { | |
try { | |
if (object.addEventListener){ | |
object.addEventListener(event, method, false); | |
} else if(object.attachEvent){ | |
object.attachEvent('on'+event, function(){ method(window.event); }); | |
} | |
} catch (error) { | |
console.log(error.message); | |
} | |
} | |
window.hotkeys = hotkeys; | |
})(window); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment