-
Notifications
You must be signed in to change notification settings - Fork 26
/
shortcut.js
38 lines (31 loc) · 919 Bytes
/
shortcut.js
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
;(function() {
var keyCodeMap = {
'.': 'shortcutLoadItems',
'/': 'shortcutSearchItems',
'?': 'shortcutShowHelp',
'j': 'shortcutNextItem',
'k': 'shortcutPrevItem',
'n': 'shortcutNewItem',
'o': 'shortcutOpenItem',
};
var eventBlacklist = {
'input': true,
'textarea': true,
'select': true
};
document.addEventListener('keypress', function(e) {
var tagName = document.activeElement.tagName.toLowerCase();
var shortcut = keyCodeMap[String.fromCharCode(e.charCode)];
var shortcutEvent;
if (typeof shortcut === undefined || eventBlacklist[tagName]) {
return;
}
if (window.CustomEvent) {
shortcutEvent = new CustomEvent(shortcut);
} else {
shortcutEvent = document.createEvent('CustomEvent');
shortcutEvent.initCustomEvent(shortcut, false, false, null);
}
document.dispatchEvent(shortcutEvent);
});
})();