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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
|
// Copyright (c) 2010 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 BASE_KEYBOARD = {
top: 0,
left: 0,
width: 1237,
height: 468
};
var BASE_INSTRUCTIONS = {
top: 174,
left: 370,
width: 498,
height: 81
};
var LABEL_TO_KEY_TEXT = {
alt: 'alt',
backspace: 'backspace',
ctrl: 'ctrl',
enter: 'enter',
esc: 'esc',
glyph_arrow_down: 'down',
glyph_arrow_left: 'left',
glyph_arrow_right: 'right',
glyph_arrow_up: 'up',
glyph_back: 'back',
glyph_backspace: 'backspace',
glyph_brightness_down: 'bright down',
glyph_brightness_up: 'bright up',
glyph_enter: 'enter',
glyph_forward: 'forward',
glyph_fullscreen: 'fullscreen',
glyph_overview: 'windows',
glyph_power: 'power',
glyph_reload: 'reload',
glyph_reload: 'reload',
glyph_search: 'search',
glyph_tab: 'tab',
glyph_tools: 'tools',
glyph_volume_down: 'vol. down',
glyph_volume_mute: 'mute',
glyph_volume_up: 'vol. up',
shift: 'shift',
tab: 'tab'
};
var MODIFIER_TO_CLASS = {
'SHIFT': 'modifier-shift',
'CTRL': 'modifier-ctrl',
'ALT': 'modifier-alt'
};
var IDENTIFIER_TO_CLASS = {
'2A': 'is-shift',
'1D': 'is-ctrl',
'38': 'is-alt'
};
var keyboardOverlayId = 'en_US';
/**
* Returns layouts data.
*/
function getLayouts() {
return keyboardOverlayData['layouts'];
}
/**
* Returns shortcut data.
*/
function getShortcutData() {
return keyboardOverlayData['shortcut'];
}
/**
* Returns the keyboard overlay ID.
*/
function getKeyboardOverlayId() {
return keyboardOverlayId
}
/**
* Returns keyboard glyph data.
*/
function getKeyboardGlyphData() {
return keyboardOverlayData['keyboardGlyph'][getKeyboardOverlayId()];
}
/**
* Converts a single hex number to a character.
*/
function hex2char(hex) {
if (!hex) {
return '';
}
var result = '';
var n = parseInt(hex, 16);
if (n <= 0xFFFF) {
result += String.fromCharCode(n);
} else if (n <= 0x10FFFF) {
n -= 0x10000;
result += (String.fromCharCode(0xD800 | (n >> 10)) +
String.fromCharCode(0xDC00 | (n & 0x3FF)));
} else {
console.error('hex2Char error: Code point out of range :' + hex);
}
return result;
}
/**
* Returns a list of modifiers from the key event.
*/
function getModifiers(e) {
if (!e) {
return [];
}
var modifiers = [];
if (e.keyCode == 16 || e.shiftKey) {
modifiers.push('SHIFT');
}
if (e.keyCode == 17 || e.ctrlKey) {
modifiers.push('CTRL');
}
if (e.keyCode == 18 || e.altKey) {
modifiers.push('ALT');
}
return modifiers.sort();
}
/**
* Returns an ID of the key.
*/
function keyId(identifier, i) {
return identifier + '-key-' + i;
}
/**
* Returns an ID of the text on the key.
*/
function keyTextId(identifier, i) {
return identifier + '-key-text-' + i;
}
/**
* Returns an ID of the shortcut text.
*/
function shortcutTextId(identifier, i) {
return identifier + '-shortcut-text-' + i;
}
/**
* Returns true if |list| contains |e|.
*/
function contains(list, e) {
return list.indexOf(e) != -1;
}
/**
* Returns a list of the class names corresponding to the identifier and
* modifiers.
*/
function getKeyClasses(identifier, modifiers) {
var classes = ['keyboard-overlay-key'];
for (var i = 0; i < modifiers.length; ++i) {
classes.push(MODIFIER_TO_CLASS[modifiers[i]]);
}
if ((identifier == '2A' && contains(modifiers, 'SHIFT')) ||
(identifier == '1D' && contains(modifiers, 'CTRL')) ||
(identifier == '38' && contains(modifiers, 'ALT'))) {
classes.push('pressed');
classes.push(IDENTIFIER_TO_CLASS[identifier]);
}
return classes;
}
/**
* Returns true if a character is a ASCII character.
*/
function isAscii(c) {
var charCode = c.charCodeAt(0);
return 0x00 <= charCode && charCode <= 0x7F;
}
/**
* Returns a label of the key.
*/
function getKeyLabel(keyData, modifiers) {
if (!keyData) {
return '';
}
if (keyData.label in LABEL_TO_KEY_TEXT) {
return LABEL_TO_KEY_TEXT[keyData.label];
}
var keyLabel = '';
for (var j = 1; j <= 9; j++) {
var pos = keyData['p' + j];
if (!pos) {
continue;
}
if (LABEL_TO_KEY_TEXT[pos]) {
return LABEL_TO_KEY_TEXT[pos];
}
keyLabel = hex2char(pos);
if (!keyLabel) {
continue;
}
if (isAscii(keyLabel) &&
getShortcutData()[getAction(keyLabel, modifiers)]) {
break;
}
}
return keyLabel;
}
/**
* Returns a normalized string used for a key of shortcutData.
*/
function getAction(keycode, modifiers) {
return [keycode].concat(modifiers).join(' ');
}
/**
* Returns a text which displayed on a key.
*/
function getKeyTextValue(keyData) {
if (LABEL_TO_KEY_TEXT[keyData.label]) {
return LABEL_TO_KEY_TEXT[keyData.label];
}
var chars = [];
for (var j = 1; j <= 9; ++j) {
var pos = keyData['p' + j];
if (LABEL_TO_KEY_TEXT[pos]) {
return LABEL_TO_KEY_TEXT[pos];
}
if (pos && pos.length > 0) {
chars.push(hex2char(pos));
}
}
return chars.join(' ');
}
/**
* Updates the whole keyboard.
*/
function update(e) {
var modifiers = getModifiers(e);
var instructions = document.getElementById('instructions');
if (modifiers.length == 0) {
instructions.style.visibility = 'visible';
} else {
instructions.style.visibility = 'hidden';
}
var keyboardGlyphData = getKeyboardGlyphData();
var shortcutData = getShortcutData();
var layout = getLayouts()[keyboardGlyphData.layoutName];
for (var i = 0; i < layout.length; ++i) {
var identifier = layout[i][0];
var keyData = keyboardGlyphData.keys[identifier];
var classes = getKeyClasses(identifier, modifiers, keyData);
var keyLabel = getKeyLabel(keyData, modifiers);
var shortcutId = shortcutData[getAction(keyLabel, modifiers)];
if (shortcutId) {
classes.push('is-shortcut');
}
var key = document.getElementById(keyId(identifier, i));
key.className = classes.join(' ');
if (!keyData) {
continue;
}
var keyText = document.getElementById(keyTextId(identifier, i));
var keyTextValue = getKeyTextValue(keyData);
if (keyTextValue) {
keyText.style.visibility = 'visible';
} else {
keyText.style.visibility = 'hidden';
}
keyText.textContent = keyTextValue;
var shortcutText = document.getElementById(shortcutTextId(identifier, i));
if (shortcutId) {
shortcutText.style.visibility = 'visible';
shortcutText.textContent = templateData[shortcutId];
} else {
shortcutText.style.visibility = 'hidden';
}
if (keyData.format) {
var format = keyData.format;
if (format == 'left' || format == 'right') {
shortcutText.style.textAlign = format;
keyText.style.textAlign = format;
}
}
}
}
/**
* A callback furnction for the onkeydown event.
*/
function keydown(e) {
if (!getKeyboardOverlayId()) {
return;
}
update(e);
}
/**
* A callback furnction for the onkeyup event.
*/
function keyup(e) {
if (!getKeyboardOverlayId()) {
return;
}
update();
}
/**
* Initializes the layout of the keys.
*/
function initLayout() {
var layout = getLayouts()[getKeyboardGlyphData().layoutName];
var keyboard = document.body;
var minX = window.innerWidth;
var maxX = 0;
var minY = window.innerHeight;
var maxY = 0;
var multiplier = 1.38 * window.innerWidth / BASE_KEYBOARD.width;
var keyMargin = 7;
var offsetX = 10;
var offsetY = 7;
for (var i = 0; i < layout.length; i++) {
var array = layout[i];
var identifier = array[0];
var x = Math.round((array[1] + offsetX) * multiplier);
var y = Math.round((array[2] + offsetY) * multiplier);
var w = Math.round((array[3] - keyMargin) * multiplier);
var h = Math.round((array[4] - keyMargin) * multiplier);
var key = document.createElement('div');
key.id = keyId(identifier, i);
key.className = 'keyboard-overlay-key';
key.style.left = x + 'px';
key.style.top = y + 'px';
key.style.width = w + 'px';
key.style.height = h + 'px';
var keyText = document.createElement('div');
keyText.id = keyTextId(identifier, i);
keyText.className = 'keyboard-overlay-key-text';
keyText.style.visibility = 'hidden';
key.appendChild(keyText);
var shortcutText = document.createElement('div');
shortcutText.id = shortcutTextId(identifier, i);
shortcutText.className = 'keyboard-overlay-shortcut-text';
shortcutText.style.visilibity = 'hidden';
key.appendChild(shortcutText);
keyboard.appendChild(key);
minX = Math.min(minX, x);
maxX = Math.max(maxX, x + w);
minY = Math.min(minY, y);
maxY = Math.max(maxY, y + h);
}
var width = maxX - minX + 1;
var height = maxY - minY + 1;
keyboard.style.width = (width + 2 * (minX + 1)) + 'px';
keyboard.style.height = (height + 2 * (minY + 1)) + 'px';
var instructions = document.createElement('instructions');
instructions = document.createElement('div');
instructions.id = 'instructions';
instructions.className = 'keyboard-overlay-instructions';
instructions.style.left = ((BASE_INSTRUCTIONS.left - BASE_KEYBOARD.left) *
width / BASE_KEYBOARD.width + minX) + 'px';
instructions.style.top = ((BASE_INSTRUCTIONS.top - BASE_KEYBOARD.top) *
height / BASE_KEYBOARD.height + minY) + 'px';
instructions.style.width = (width * BASE_INSTRUCTIONS.width /
BASE_KEYBOARD.width) + 'px';
instructions.style.height = (height * BASE_INSTRUCTIONS.height /
BASE_KEYBOARD.height) + 'px';
var instructionsText = document.createElement('span');
instructionsText.id = 'instructions-text';
instructionsText.className = 'keyboard-overlay-instructions-text';
instructionsText.innerHTML = templateData.keyboardOverlayInstructions;
instructions.appendChild(instructionsText);
keyboard.appendChild(instructions);
}
/**
* A callback function for the onload event of the body element.
*/
function init() {
document.addEventListener('keydown', keydown);
document.addEventListener('keyup', keyup);
chrome.send('getKeyboardOverlayId');
}
/**
* Initializes the global keyboad overlay ID and the layout of keys.
* Called after sending the 'getKeyboardOverlayId' message.
*/
function initKeyboardOverlayId(overlayId) {
keyboardOverlayId = overlayId;
while(document.body.firstChild) {
document.body.removeChild(document.body.firstChild);
}
initLayout();
update();
}
document.addEventListener('DOMContentLoaded', init);
|