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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
|
// 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.
/**
* @fileoverview A simple virtual keyboard implementation.
*/
var KEY_MODE = 'key';
var SHIFT_MODE = 'shift';
var NUMBER_MODE = 'number';
var SYMBOL_MODE = 'symbol';
var MODES = [ KEY_MODE, SHIFT_MODE, NUMBER_MODE, SYMBOL_MODE ];
var currentMode = KEY_MODE;
var MODE_TRANSITIONS = {};
MODE_TRANSITIONS[KEY_MODE + SHIFT_MODE] = SHIFT_MODE;
MODE_TRANSITIONS[KEY_MODE + NUMBER_MODE] = NUMBER_MODE;
MODE_TRANSITIONS[SHIFT_MODE + SHIFT_MODE] = KEY_MODE;
MODE_TRANSITIONS[SHIFT_MODE + NUMBER_MODE] = NUMBER_MODE;
MODE_TRANSITIONS[NUMBER_MODE + SHIFT_MODE] = SYMBOL_MODE;
MODE_TRANSITIONS[NUMBER_MODE + NUMBER_MODE] = KEY_MODE;
MODE_TRANSITIONS[SYMBOL_MODE + SHIFT_MODE] = NUMBER_MODE;
MODE_TRANSITIONS[SYMBOL_MODE + NUMBER_MODE] = KEY_MODE;
/**
* The repeat delay in milliseconds before a key starts repeating. Use the same
* rate as Chromebook. (See chrome/browser/chromeos/language_preferences.cc)
* @type {number}
*/
var REPEAT_DELAY_MSEC = 500;
/**
* The repeat interval or number of milliseconds between subsequent keypresses.
* Use the same rate as Chromebook.
* @type {number}
*/
var REPEAT_INTERVAL_MSEC = 50;
/**
* A structure to track the currently repeating key on the keyboard.
*/
var repeatKey = {
/**
* The timer for the delay before repeating behaviour begins.
* @type {number|undefined}
*/
timer: undefined,
/**
* The interval timer for issuing keypresses of a repeating key.
* @type {number|undefined}
*/
interval: undefined,
/**
* The key which is currently repeating.
* @type {BaseKey|undefined}
*/
key: undefined,
/**
* Cancel the repeat timers of the currently active key.
*/
cancel: function() {
clearTimeout(this.timer);
clearInterval(this.interval);
this.timer = undefined;
this.interval = undefined;
this.key = undefined;
}
};
/**
* Transition the mode according to the given transition.
* @param {string} transition The transition to take.
* @return {void}
*/
function transitionMode(transition) {
currentMode = MODE_TRANSITIONS[currentMode + transition];
setMode(currentMode);
}
/**
* Send the given key to chrome, via the experimental extension API.
* @param {string} key The key to send.
* @param {string=} opt_type The type of event to send (keydown or keyup). If
* omitted send both keydown and keyup events.
* @return {void}
*/
function sendKey(key, type) {
var keyEvent = {'keyIdentifier': key};
if (!type || type == 'keydown') {
// A keypress event is automatically generated for printable characters
// immediately following the keydown event.
keyEvent.type = 'keydown';
if (chrome.experimental) {
chrome.experimental.input.sendKeyboardEvent(keyEvent);
}
}
if (!type || type == 'keyup') {
keyEvent.type = 'keyup';
if (chrome.experimental) {
chrome.experimental.input.sendKeyboardEvent(keyEvent);
}
// Exit shift mode after completing any shifted keystroke.
if (currentMode == SHIFT_MODE) {
transitionMode(SHIFT_MODE);
}
}
}
/**
* Set up the event handlers necessary to respond to mouse and touch events on
* the virtual keyboard.
* @param {BaseKey} key The BaseKey object corresponding to this key.
* @param {Element} element The top-level DOM Element to set event handlers on.
* @param {function()} keyDownHandler The event handler called when the key is
* pressed. This will be called repeatedly when holding a repeating key.
* @param {function()=} opt_keyUpHandler The event handler called when the key
* is released. This is only called once per actual key press.
*/
function setupKeyEventHandlers(key, element, keyDownHandler, opt_keyUpHandler) {
/**
* Handle a key down event on the virtual key.
* @param {UIEvent} evt The UI event which triggered the key down.
*/
var downHandler = function(evt) {
// Don't process a key down if the key is already down.
if (key.pressed) {
return;
}
key.pressed = true;
if (keyDownHandler) {
keyDownHandler();
}
evt.preventDefault();
repeatKey.cancel();
// Start a repeating timer if there is a repeat interval and a function to
// process key down events.
if (key.repeat && keyDownHandler) {
repeatKey.key = key;
// The timeout for the repeating timer occurs at
// REPEAT_DELAY_MSEC - REPEAT_INTERVAL_MSEC so that the interval
// function can handle all repeat keypresses and will get the first one
// at the correct time.
repeatKey.timer = setTimeout(function() {
repeatKey.timer = undefined;
repeatKey.interval = setInterval(function() {
keyDownHandler();
}, REPEAT_INTERVAL_MSEC);
}, Math.max(0, REPEAT_DELAY_MSEC - REPEAT_INTERVAL_MSEC));
}
};
/**
* Handle a key up event on the virtual key.
* @param {UIEvent} evt The UI event which triggered the key up.
*/
var upHandler = function(evt) {
// If they key was not actually pressed do not send a key up event.
if (!key.pressed) {
return;
}
key.pressed = false;
// Cancel running repeat timer for the released key only.
if (repeatKey.key == key) {
repeatKey.cancel();
}
if (opt_keyUpHandler) {
opt_keyUpHandler();
}
evt.preventDefault();
};
// Setup mouse event handlers.
element.addEventListener('mousedown', downHandler);
element.addEventListener('mouseup', upHandler);
element.addEventListener('mouseout', upHandler);
// Setup touch handlers.
element.addEventListener('touchstart', downHandler);
element.addEventListener('touchend', upHandler);
}
/**
* Create closure for the sendKey function.
* @param {string} key The key paramater to sendKey.
* @param {string=} type The type parameter to sendKey.
* @return {function()} A function which calls sendKey(key, type).
*/
function sendKeyFunction(key, type) {
return function() {
sendKey(key, type);
};
}
/**
* Plain-old-data class to represent a character.
* @param {string} display The HTML to be displayed.
* @param {string} id The key identifier for this Character.
* @constructor
*/
function Character(display, id) {
this.display = display;
this.keyIdentifier = id;
}
/**
* Convenience function to make the keyboard data more readable.
* @param {string} display Both the display and id for the created Character.
*/
function C(display) {
return new Character(display, display);
}
/**
* An abstract base-class for all keys on the keyboard.
* @constructor
*/
function BaseKey() {}
BaseKey.prototype = {
/**
* The aspect ratio of this key.
* @type {number}
*/
aspect_: 1,
/**
* The column.
* @type {number}
*/
column_: 0.,
/**
* The cell type of this key. Determines the background colour.
* @type {string}
*/
cellType_: '',
/**
* If true, holding this key will issue repeat keypresses.
* @type {boolean}
*/
repeat_: false,
/**
* Track the pressed state of the key. This is true if currently pressed.
* @type {boolean}
*/
pressed_: false,
/**
* Get the repeat behaviour of the key.
* @return {boolean} True if the key will repeat.
*/
get repeat() {
return this.repeat_;
},
/**
* Set the repeat behaviour of the key
* @param {boolean} repeat True if the key should repeat.
*/
set repeat(repeat) {
this.repeat_ = repeat;
},
/**
* Get the pressed state of the key.
* @return {boolean} True if the key is currently pressed.
*/
get pressed() {
return this.pressed_;
},
/**
* Set the pressed state of the key.
* @param {boolean} pressed True if the key is currently pressed.
*/
set pressed(pressed) {
this.pressed_ = pressed;
},
/**
* @return {number} The aspect ratio of this key.
*/
get aspect() {
return this.aspect_;
},
/**
* Set the horizontal position for this key.
* @param {number} column Horizonal position (in multiples of row height).
*/
set column(column) {
this.column_ = column;
},
/**
* Set the vertical position, aka row, of this key.
* @param {number} row The row.
*/
set row(row) {
for (var i in this.modeElements_) {
this.modeElements_[i].classList.add(this.cellType_ + 'r' + row);
}
},
/**
* Returns the amount of padding for the top of the key.
* @param {string} mode The mode for the key.
* @param {number} height The height of the key.
* @return {number} Padding in pixels.
*/
getPadding: function(mode, height) {
return Math.floor(height / 3.5);
},
/**
* Size the DOM elements of this key.
* @param {string} mode The mode to be sized.
* @param {number} height The height of the key.
* @return {void}
*/
sizeElement: function(mode, height) {
var padding = this.getPadding(mode, height);
var border = 1;
var margin = 5;
var width = Math.floor(height * (this.column_ + this.aspect_)) -
Math.floor(height * this.column_);
var extraHeight = margin + padding + 2 * border;
var extraWidth = margin + 2 * border;
this.modeElements_[mode].style.width = (width - extraWidth) + 'px';
this.modeElements_[mode].style.height = (height - extraHeight) + 'px';
this.modeElements_[mode].style.marginLeft = margin + 'px';
this.modeElements_[mode].style.fontSize = (height / 3.5) + 'px';
this.modeElements_[mode].style.paddingTop = padding + 'px';
},
/**
* Resize all modes of this key based on the given height.
* @param {number} height The height of the key.
* @return {void}
*/
resize: function(height) {
for (var i in this.modeElements_) {
this.sizeElement(i, height);
}
},
/**
* Create the DOM elements for the given keyboard mode. Must be overridden.
* @param {string} mode The keyboard mode to create elements for.
* @param {number} height The height of the key.
* @return {Element} The top-level DOM Element for the key.
*/
makeDOM: function(mode, height) {
throw new Error('makeDOM not implemented in BaseKey');
},
};
/**
* A simple key which displays Characters.
* @param {Character} key The Character for KEY_MODE.
* @param {Character} shift The Character for SHIFT_MODE.
* @param {Character} num The Character for NUMBER_MODE.
* @param {Character} symbol The Character for SYMBOL_MODE.
* @constructor
* @extends {BaseKey}
*/
function Key(key, shift, num, symbol) {
this.modeElements_ = {};
this.aspect_ = 1; // ratio width:height
this.cellType_ = '';
this.modes_ = {};
this.modes_[KEY_MODE] = key;
this.modes_[SHIFT_MODE] = shift;
this.modes_[NUMBER_MODE] = num;
this.modes_[SYMBOL_MODE] = symbol;
}
Key.prototype = {
__proto__: BaseKey.prototype,
/** @inheritDoc */
makeDOM: function(mode, height) {
this.modeElements_[mode] = document.createElement('div');
this.modeElements_[mode].textContent = this.modes_[mode].display;
this.modeElements_[mode].className = 'key';
this.sizeElement(mode, height);
setupKeyEventHandlers(this, this.modeElements_[mode],
sendKeyFunction(this.modes_[mode].keyIdentifier, 'keydown'),
sendKeyFunction(this.modes_[mode].keyIdentifier, 'keyup'));
return this.modeElements_[mode];
}
};
/**
* A key which displays an SVG image.
* @param {number} aspect The aspect ratio of the key.
* @param {string} className The class that provides the image.
* @param {string} keyId The key identifier for the key.
* @constructor
* @extends {BaseKey}
*/
function SvgKey(aspect, className, keyId) {
this.modeElements_ = {};
this.aspect_ = aspect;
this.cellType_ = 'nc';
this.className_ = className;
this.keyId_ = keyId;
}
SvgKey.prototype = {
__proto__: BaseKey.prototype,
/** @inheritDoc */
getPadding: function(mode, height) { return 0; },
/** @inheritDoc */
makeDOM: function(mode, height) {
this.modeElements_[mode] = document.createElement('div');
this.modeElements_[mode].className = 'key';
var img = document.createElement('div');
img.className = 'image-key ' + this.className_;
this.modeElements_[mode].appendChild(img);
setupKeyEventHandlers(this, this.modeElements_[mode],
sendKeyFunction(this.keyId_, 'keydown'),
sendKeyFunction(this.keyId_, 'keyup'));
this.sizeElement(mode, height);
return this.modeElements_[mode];
}
};
/**
* A Key that remains the same through all modes.
* @param {number} aspect The aspect ratio of the key.
* @param {string} content The display text for the key.
* @param {string} keyId The key identifier for the key.
* @constructor
* @extends {BaseKey}
*/
function SpecialKey(aspect, content, keyId) {
this.modeElements_ = {};
this.aspect_ = aspect;
this.cellType_ = 'nc';
this.content_ = content;
this.keyId_ = keyId;
}
SpecialKey.prototype = {
__proto__: BaseKey.prototype,
/** @inheritDoc */
makeDOM: function(mode, height) {
this.modeElements_[mode] = document.createElement('div');
this.modeElements_[mode].textContent = this.content_;
this.modeElements_[mode].className = 'key';
this.sizeElement(mode, height);
setupKeyEventHandlers(this, this.modeElements_[mode],
sendKeyFunction(this.keyId_, 'keydown'),
sendKeyFunction(this.keyId_, 'keyup'));
return this.modeElements_[mode];
}
};
/**
* A shift key.
* @param {number} aspect The aspect ratio of the key.
* @constructor
* @extends {BaseKey}
*/
function ShiftKey(aspect) {
this.modeElements_ = {};
this.aspect_ = aspect;
this.cellType_ = 'nc';
}
ShiftKey.prototype = {
__proto__: BaseKey.prototype,
/** @inheritDoc */
getPadding: function(mode, height) {
if (mode == NUMBER_MODE || mode == SYMBOL_MODE) {
return BaseKey.prototype.getPadding.call(this, mode, height);
}
return 0;
},
/** @inheritDoc */
makeDOM: function(mode, height) {
this.modeElements_[mode] = document.createElement('div');
if (mode == KEY_MODE || mode == SHIFT_MODE) {
var shift = document.createElement('div');
shift.className = 'image-key shift';
this.modeElements_[mode].appendChild(shift);
} else if (mode == NUMBER_MODE) {
this.modeElements_[mode].textContent = 'more';
} else if (mode == SYMBOL_MODE) {
this.modeElements_[mode].textContent = '#123';
}
if (mode == SHIFT_MODE || mode == SYMBOL_MODE) {
this.modeElements_[mode].className = 'moddown key';
} else {
this.modeElements_[mode].className = 'key';
}
this.sizeElement(mode, height);
setupKeyEventHandlers(this, this.modeElements_[mode],
function() {
transitionMode(SHIFT_MODE);
});
return this.modeElements_[mode];
},
};
/**
* The symbol key: switches the keyboard into symbol mode.
* @constructor
* @extends {BaseKey}
*/
function SymbolKey() {
this.modeElements_ = {}
this.aspect_ = 1.3;
this.cellType_ = 'nc';
}
SymbolKey.prototype = {
__proto__: BaseKey.prototype,
/** @inheritDoc */
makeDOM: function(mode, height) {
this.modeElements_[mode] = document.createElement('div');
if (mode == KEY_MODE || mode == SHIFT_MODE) {
this.modeElements_[mode].textContent = '#123';
} else if (mode == NUMBER_MODE || mode == SYMBOL_MODE) {
this.modeElements_[mode].textContent = 'abc';
}
if (mode == NUMBER_MODE || mode == SYMBOL_MODE) {
this.modeElements_[mode].className = 'moddown key';
} else {
this.modeElements_[mode].className = 'key';
}
this.sizeElement(mode, height);
setupKeyEventHandlers(this, this.modeElements_[mode],
function() {
transitionMode(NUMBER_MODE);
});
return this.modeElements_[mode];
}
};
/**
* The ".com" key.
* @constructor
* @extends {BaseKey}
*/
function DotComKey() {
this.modeElements_ = {}
this.aspect_ = 1.3;
this.cellType_ = 'nc';
}
DotComKey.prototype = {
__proto__: BaseKey.prototype,
/** @inheritDoc */
makeDOM: function(mode, height) {
this.modeElements_[mode] = document.createElement('div');
this.modeElements_[mode].textContent = '.com';
this.modeElements_[mode].className = 'key';
this.sizeElement(mode, height);
setupKeyEventHandlers(this, this.modeElements_[mode],
function() {
sendKey('.');
sendKey('c');
sendKey('o');
sendKey('m');
});
return this.modeElements_[mode];
}
};
/**
* The key that hides the keyboard.
* @param {number} aspect The aspect ratio of the key.
* @constructor
* @extends {BaseKey}
*/
function HideKeyboardKey(aspect) {
this.modeElements_ = {}
this.aspect_ = aspect;
this.cellType_ = 'nc';
}
HideKeyboardKey.prototype = {
__proto__: BaseKey.prototype,
/** @inheritDoc */
getPadding: function(mode, height) { return 0; },
/** @inheritDoc */
makeDOM: function(mode, height) {
this.modeElements_[mode] = document.createElement('div');
this.modeElements_[mode].className = 'key';
var hide = document.createElement('div');
hide.className = 'image-key hide';
this.modeElements_[mode].appendChild(hide);
this.sizeElement(mode, height);
setupKeyEventHandlers(this, this.modeElements_[mode],
function() {
if (chrome.experimental) {
chrome.experimental.input.hideKeyboard();
}
});
return this.modeElements_[mode];
}
};
/**
* A container for keys.
* @param {number} position The position of the row (0-3).
* @param {Array.<BaseKey>} keys The keys in the row.
* @constructor
*/
function Row(position, keys) {
this.position_ = position;
this.keys_ = keys;
this.element_ = null;
this.modeElements_ = {};
}
Row.prototype = {
/**
* Get the total aspect ratio of the row.
* @return {number} The aspect ratio relative to a height of 1 unit.
*/
get aspect() {
var total = 0;
for (var i = 0; i < this.keys_.length; ++i) {
total += this.keys_[i].aspect;
}
return total;
},
/**
* Create the DOM elements for the row.
* @return {Element} The top-level DOM Element for the row.
*/
makeDOM: function(height) {
this.element_ = document.createElement('div');
this.element_.className = 'row';
for (var i = 0; i < MODES.length; ++i) {
var mode = MODES[i];
this.modeElements_[mode] = document.createElement('div');
this.modeElements_[mode].style.display = 'none';
this.element_.appendChild(this.modeElements_[mode]);
}
for (var j = 0; j < this.keys_.length; ++j) {
var key = this.keys_[j];
for (var i = 0; i < MODES.length; ++i) {
this.modeElements_[MODES[i]].appendChild(key.makeDOM(MODES[i], height));
}
}
for (var i = 0; i < MODES.length; ++i) {
var clearingDiv = document.createElement('div');
clearingDiv.style.clear = 'both';
this.modeElements_[MODES[i]].appendChild(clearingDiv);
}
var column = 0.;
for (var i = 0; i < this.keys_.length; ++i) {
this.keys_[i].row = this.position_;
this.keys_[i].column = column;
column += this.keys_[i].aspect;
}
return this.element_;
},
/**
* Shows the given mode.
* @param {string} mode The mode to show.
* @return {void}
*/
showMode: function(mode) {
for (var i = 0; i < MODES.length; ++i) {
this.modeElements_[MODES[i]].style.display = 'none';
}
this.modeElements_[mode].style.display = 'block';
},
/**
* Resizes all keys in the row according to the global size.
* @param {number} height The height of the key.
* @return {void}
*/
resize: function(height) {
for (var i = 0; i < this.keys_.length; ++i) {
this.keys_[i].resize(height);
}
},
};
|