blob: 403af03e5fd7684e6c6d27faf2ce59c7aba22650 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
define(function () {
var shortcut = {};
shortcut._allShortcuts = [];
shortcut.add = function (modifiersString, key, callback) {
// Parse the modifiers. For example "Ctrl+Alt" will become
// {'ctrlKey': true, 'altKey': true, 'shiftKey': false}
var modifiersList = modifiersString.toLowerCase().split("+");
var modifiers = {
'ctrlKey': modifiersList.indexOf('ctrl') >= 0,
'altKey': modifiersList.indexOf('alt') >= 0,
'shiftKey': modifiersList.indexOf('shift') >= 0
};
this._allShortcuts.push({
'modifiers': modifiers,
'key': key.toLowerCase(),
'callback': callback
});
};
document.onkeypress = function (e) {
e = e || window.event;
var modifiers = {
'ctrlKey': e.ctrlKey,
'altKey': e.altKey,
'shiftKey': e.shiftKey
};
// Obtain the key
var charCode;
if (typeof e.which == "number") {
charCode = e.which;
} else {
charCode = e.keyCode;
}
var key = String.fromCharCode(charCode).toLowerCase();
// Search for a matching shortcut
for (i = 0; i < shortcut._allShortcuts.length; i += 1) {
var currentShortcut = shortcut._allShortcuts[i];
var match = currentShortcut.key == key &&
currentShortcut.modifiers.ctrlKey == modifiers.ctrlKey &&
currentShortcut.modifiers.altKey == modifiers.altKey &&
currentShortcut.modifiers.shiftKey == modifiers.shiftKey;
if (match) {
currentShortcut.callback();
return;
}
}
};
return shortcut;
});
|