forked from GoogleChrome/chrome-extensions-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keycodes.js
94 lines (90 loc) · 1.65 KB
/
keycodes.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* Copyright (c) 2011 The Chromium Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
var KEY_MAP = {
12: 'Clear',
14: 'Enter',
33: 'PgUp',
34: 'PgDown',
35: 'End',
36: 'Home',
37: 'Left',
38: 'Up',
39: 'Right',
40: 'Down',
45: 'Insert',
46: 'Delete',
96: 'Numpad0',
97: 'Numpad1',
98: 'Numpad2',
99: 'Numpad3',
100: 'Numpad4',
101: 'Numpad5',
102: 'Numpad6',
103: 'Numpad7',
104: 'Numpad8',
105: 'Numpad9',
106: '*',
107: 'Plus',
108: '_',
109: '-',
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',
124: 'F13',
125: 'F14',
126: 'F15',
186: ';',
187: '=',
188: ',',
189: '-',
190: '.',
191: '/',
192: '`',
219: '[',
221: ']'
};
var isMac = (navigator.appVersion.indexOf("Mac") != -1);
function keyEventToString(evt) {
var tokens = [];
if (evt.ctrlKey) {
tokens.push('Control');
}
if (evt.altKey) {
tokens.push(isMac ? 'Option' : 'Alt');
}
if (evt.metaKey) {
tokens.push(isMac ? 'Command' : 'Meta');
}
if (evt.shiftKey) {
tokens.push('Shift');
}
if (evt.keyCode >= 48 && evt.keyCode <= 90) {
tokens.push(String.fromCharCode(evt.keyCode));
} else if (KEY_MAP[evt.keyCode]) {
tokens.push(KEY_MAP[evt.keyCode]);
} else {
return '';
}
return tokens.join('+');
}
function getDefaultKeyString() {
return keyEventToString({
keyCode: 83, // 's'
shiftKey: true,
altKey: true,
ctrlKey: true,
metaKey: false});
}