-
Notifications
You must be signed in to change notification settings - Fork 1
/
virtualkeyboard.js
2151 lines (2049 loc) · 70.1 KB
/
virtualkeyboard.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
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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* $Id$
* $HeadURL$
*
* Virtual Keyboard.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* See http://www.gnu.org/copyleft/lesser.html
*
* LGPL licence is applicable when you keep copyrights visible on the keyboard UI.
*
* Do not remove this comment if you want to use script!
*
* This software is protected by patent No.2009611147 issued on 20.02.2009 by Russian Federal Service for Intellectual Property Patents and Trademarks.
*
* @author Ilya Lebedev
* @copyright 2006-2011 Ilya Lebedev <[email protected]>
* @version $Rev$
* @lastchange $Author$ $Date$
* @class VirtualKeyboard
* @constructor
*/
var VirtualKeyboard = new function () {
var self = this;
self.$VERSION$ = "{{VERSION}}";
/**
* Path to the keyboard install root
*
* @type String
* @scope private
*/
var basePath = findPath('vk_loader.js');
/**
* Regexp to test a char against to prove it is a dead key
*
* @type RegExp
* @scope private
*/
var DK_REG = /\x03/;
/**
* Some configurable stuff
*
* @type Object
* @scope private
*/
var options = {
'layout' : null
,'skin' : 'winxp'
}
/**
* ID prefix
*
* @type String
* @scope private
*/
var idPrefix = 'kb_b';
/**
* This flag is used to enable or disable keyboard animation
* This is very useful in the secure environments, like password input. Controlled by the CSS class on the field
*
* @see cssClasses
* @type Boolean
* @scope private
*/
var animate = true;
/**
* This flag is used to check if keyboard is availble for operations (i.e. it does not wait for resource loading)
*
* @type Boolean
* @scope private
*/
var enabled = true;
/**
* list of the control keys to be shown
*
* @type Object
* @scope private
*/
var controlKeys = {14:'backspace'
,15:'tab'
,28:'enter'
,29:'caps'
,41:'shift_left'
,52:'shift_right'
,53:'del'
,54:'ctrl_left'
,55:'alt_left'
,56:'space'
,57:'alt_right'
,58:'ctrl_right'};
/**
* Prefixes for the keys
*
* @type Object
* @scope private
*/
var KEY = {
'SHIFT' : 'shift'
,'ALT' : 'alt'
,'CTRL' : 'ctrl'
,'CAPS' : 'caps'
}
/**
* Current keyboard mapping
*
* @type Array
* @scope private
*/
var keymap;
/**
* List of the available mappings
*
* @type Object
* @scope private
*/
var keymaps = {
'QWERTY Default' : "À1234567890m=ÜQWERTYUIOPÛÝASDFGHJKL;ÞZXCVBNM¼¾¿"
,'QWERTY Canadian' : "Þ1234567890m=ÜQWERTYUIOPÛÝASDFGHJKL;ÀZXCVBNM¼¾¿"
,'QWERTY Dutch' : "Þ1234567890Û¿ÜQWERTYUIOPÝ;ASDFGHJKL=ÀZXCVBNM¼¾m"
,'QWERTY Estonian' : "¿1234567890m=ÜQWERTYUIOPÞÛASDFGHJKL;ÀZXCVBNM¼¾Ý"
,'QWERTY Greek (220)' : "À1234567890¿ÛÜQWERTYUIOP=ÝASDFGHJKL;ÞZXCVBNM¼¾m"
,'QWERTY Greek (319)' : "À1234567890¿=ÜQWERTYUIOPÛÝASDFGHJKL;ÞZXCVBNM¼¾m"
,'QWERTY Gujarati' : "À1234567890m=XQWERTYUIOPÛÝASDFGHJKL;ÜZXCVBNM¼¾¿"
,'QWERTY Italian' : "Ü1234567890ÛÝ¿QWERTYUIOP;=ASDFGHJKLÀÞZXCVBNM¼¾m"
,'QWERTY Kannada' : "À1234567890m=ZQWERTYUIOPÛÝASDFGHJKL;ÞZXCVBNM¼¾¿"
,'QWERTY Portuguese' : "À1234567890ÛÝ¿QWERTYUIOP=;ASDFGHJKLÞÜZXCVBNM¼¾m"
,'QWERTY Scandinavian' : "Ü1234567890=Û¿QWERTYUIOPÝ;ASDFGHJKLÀÞZXCVBNM¼¾m"
,'QWERTY Spanish' : "Ü1234567890mÛ¿QWERTYUIOPÝ;ASDFGHJKLÀÞZXCVBNM¼¾ß"
,'QWERTY Tamil' : "À1234567890m =ZQWERTYUIOPÛÝASDFGHJKL;ÞCVBNM¼¾ ¿"
,'QWERTY Turkish' : "À1234567890ßm¼QWERTYUIOPÛÝASDFGHJKL;ÞZXCVBNM¿Ü¾"
,'QWERTY UK' : "ß1234567890m=ÞQWERTYUIOPÛÝASDFGHJKL;ÀZXCVBNM¼¾¿"
,'QWERTZ Albanian' : "À1234567890m=ÜQWERTZUIOPÛÝASDFGHJKL;ÞYXCVBNM¼¾¿"
,'QWERTZ Bosnian' : "À1234567890¿=ÜQWERTZUIOPÛÝASDFGHJKL;ÞYXCVBNM¼¾m"
,'QWERTZ Czech' : "À1234567890=¿ÜQWERTZUIOPÛÝASDFGHJKL;ÞYXCVBNM¼¾m"
,'QWERTZ German' : "Ü1234567890ÛÝ¿QWERTZUIOP;=ASDFGHJKLÀÞYXCVBNM¼¾m"
,'QWERTZ Hungarian' : "0123456789À¿=ÜQWERTZUIOPÛÝASDFGHJKL;ÞYXCVBNM¼¾m"
,'QWERTZ Slovak' : "À1234567890¿ßÜQWERTZUIOPÛÝASDFGHJKL;ÞYXCVBNM¼¾m"
,'QWERTZ Swiss' : "Ü1234567890ÛÝßQWERTZUIOP;ÞASDFGHJKLÀ¿YXCVBNM¼¾m"
,'AZERTY Belgian' : "Þ1234567890ÛmÜAZERTYUIOPÝ;QSDFGHJKLMÀWXCVBN¼¾¿="
,'AZERTY French' : "Þ1234567890Û=ÜAZERTYUIOPÝ;QSDFGHJKLMÀWXCVBN¼¾¿ß"
,',WERTY Bulgarian' : "À1234567890m¾Ü¼WERTYUIOPÛÝASDFGHJKL;ÞZXCVBNMßQ¿"
,'QGJRMV Latvian' : "À1234567890mFÜQGJRMVNZWXYH;USILDATECÞÛBÝKPOß¼¾¿"
,'/,.PYF UK-Dvorak' : "m1234567890ÛÝÜÀ¼¾PYFGCRL¿=AOEUIDHTNSÞ;QJKXBMWVZ"
,'FG;IOD Turkish F' : "À1234567890=mXFG;IODRNHPQWUÛEAÝTKMLYÞJÜVC¿ZSB¾¼"
,';QBYUR US-Dvorak' : "7ÛÝ¿PFMLJ4321Ü;QBYURSO¾65=mKCDTHEAZ8ÞÀXGVWNI¼09"
,'56Q.OR US-Dvorak' : "m1234JLMFP¿ÛÝÜ56Q¾ORSUYB;=78ZAEHTDCKÞ90X¼INWVGÀ"
}
/**
* Keyboard mode, bitmap
*
*
*
*
* @type Number
* @scope private
*/
var mode = 0
,VK_NORMAL = 0
,VK_SHIFT = 1
,VK_ALT = 2
,VK_CTRL = 4
,VK_CAPS = 8
,VK_CTRL_CAPS = VK_CTRL|VK_CAPS
,VK_CTRL_SHIFT = VK_CTRL|VK_SHIFT
,VK_ALT_CAPS = VK_ALT|VK_CAPS
,VK_ALT_CTRL = VK_ALT|VK_CTRL
,VK_ALT_CTRL_CAPS = VK_ALT|VK_CTRL|VK_CAPS
,VK_ALT_SHIFT = VK_ALT|VK_SHIFT
,VK_SHIFT_ALT_CTRL = VK_SHIFT|VK_ALT|VK_CTRL
,VK_SHIFT_CAPS = VK_SHIFT|VK_CAPS
,VK_ALL = VK_SHIFT|VK_ALT|VK_CTRL|VK_CAPS;
/**
* CSS classes will be used to style buttons
*
* @type Object
* @scope private
*/
var cssClasses = {
'buttonUp' : 'kbButton'
,'buttonDown' : 'kbButtonDown'
,'buttonHover' : 'kbButtonHover'
,'hoverShift' : 'hoverShift'
,'hoverAlt' : 'hoverAlt'
,'modeAlt' : 'modeAlt'
,'modeAltCaps' : 'modeAltCaps'
,'modeCaps' : 'modeCaps'
,'modeNormal' : 'modeNormal'
,'modeShift' : 'modeShift'
,'modeShiftAlt' : 'modeShiftAlt'
,'modeShiftAltCaps': 'modeShiftAltCaps'
,'modeShiftCaps' : 'modeShiftCaps'
,'charNormal' : 'charNormal'
,'charShift' : 'charShift'
,'charAlt' : 'charAlt'
,'charShiftAlt' : 'charShiftAlt'
,'charCaps' : 'charCaps'
,'charShiftCaps' : 'charShiftCaps'
,'hiddenAlt' : 'hiddenAlt'
,'hiddenCaps' : 'hiddenCaps'
,'hiddenShift' : 'hiddenShift'
,'hiddenShiftCaps' : 'hiddenShiftCaps'
,'deadkey' : 'deadKey'
,'noanim' : 'VK_no_animate'
}
/**
* current layout
*
* @type Object
* @scope public
*/
var lang = null;
/**
* Available layouts
*
* Structure:
* [ <char1>, <charN>]
* with the additional properties:
* .name : {String} layout name to find it using switchLayout
* .dk : {String} list of the active dead keys, matches and replacements
* .cbk : {Function} custom input transformations
* OR
* {Object} { 'activate' : optional activation (on layout select) callback
* 'charProcessor' : required input transformation callback, receiving 3 parameters:
* - current input buffer (selection)
* - processed char
* - keyboard mode object, containing fields 'shift', 'alt', 'ctrl' and 'caps' where true means this modifier active
* }
* .rtl : right-to-left or left-to-right input flag
* .keys : multi-dimensional array of the key mapping
*
* Where <char> is the array of the chars ['<normal>','<shift>','<alt>','<shift_alt>','<caps>','<shift_caps>']
*
* @type Array
* @scope private
*/
var layout = []
/**
* Name-to-ID map
*
* @type Object
* @scope private
*/
layout.hash = {};
/**
* Available layout codes
*
* @type Array
* @scope private
*/
layout.codes = {};
/**
* Filter on the layout codes
*
* @type Array
* @scope private
*/
layout.codeFilter = null;
/**
* Generated layout options
*
* @type Array
* @scope private
*/
layout.options = null;
/**
* Shortcuts to the nodes
*
* @type Object
* @scope private
*/
var nodes = {
keyboard : null // Keyboard container @type HTMLDivElement
,desk : null // Keyboard desk @type HTMLDivElement
,progressbar : null // Progressbar @type HTMLDivElement
,langbox : null // Language selector @type HTMLSelectElement
,attachedInput : null// Field, keyboard attached to
}
/**
* Key code to be inserted on the keypress
*
* @type Number
* @scope private
*/
var newKeyCode = null;
/**************************************************************************
** KEYBOARD LAYOUT
**************************************************************************/
/**
* Adds a number of layouts, passed as arguments to this function
*/
self.addLayoutList = function () {
for (var i=0, aL=arguments.length; i<aL; i++) {
try {
self.addLayout(arguments[i]);
} catch (e) {
// error, skip it
}
}
}
/**
* Add layout to the list
*
* @see #layout
* @param {Object} l layout description hash:
* { 'code' : {String} layout code in form {language-COUNTRY}
* ,'name' : {String} layout name
* ,'normal' : {String,Array} keycodes without any modifiers, empty key should be set to 0x02 in array or char from this code in string
* ,'shift' : {Object} optional shift keys, in form of <offset> : <codes>
* ,'alt' : {Object} optional altgr keys, in form of <offset> : <codes>
* ,'shift_alt' : {Object} optional shift+altgr keys, in form of <offset> : <codes>
* ,'caps' : {Object} optional caps keys, in form of <offset> : <codes>
* ,'shift_caps' : {Object} optional shift+caps keys, in form of <offset> : <codes>
* ,'dk' : {String} list of the active deadkeys in form of <char> : <deadkeys>
* ,'cbk' : {Function} char processing callback
* OR
* { 'activate' : {Function} optional activation callback (called from switchLayout)
* ,'charProcessor' : {Function} required char processing callback
* }
* }
*
* <codes> is the array or string of the symbols. Codes might contain a ligatures in the form:
* string: substring of '0x01<char1><charN>0x01'
* array: array of [<charCode1>,<charCodeN>]
*
* <deadkeys> is the string or array of the matches and replacements
* string: string of '<match1><replacement1><matchN><replacementN>
* array: array of [<matchCharCode1><replacementCharCode1><matchCharCodeN><replacementCharCodeN>]
*
*
* @scope public
*/
self.addLayout = function(l) {
var code = l.code.entityDecode().split("-")
,name = l.name.entityDecode()
,alpha = __doParse(l.normal)
if (!isArray(alpha) || 47!=alpha.length) throw new Error ('VirtualKeyboard requires \'keys\' property to be an array with 47 items, '+alpha.length+' detected. Layout code: '+code+', layout name: '+name);
/*
* overwrite keys with parsed data for future use
*/
l.code = (code[1] || code[0]);
l.name = name;
l.normal = alpha;
l.domain = code[0];
l.id = l.code+" "+l.name;
/*
* don't rearrange already existing layouts
*/
if (layout.hash.hasOwnProperty(l.id)) {
var lt = layout.hash[l.id];
for (var z in l) {
lt[z] = l[z];
}
} else {
/*
* update list of the layout codes
*/
var code;
if (!layout.codes.hasOwnProperty(l.code)) {
code = {'name' : l.code, 'layout' : []};
layout.codes[l.code] = code;
} else {
code = layout.codes[l.code];
}
layout.push(l);
code.layout.push(l);
layout.hash[l.id] = l;
/*
* update list of the layout codes
*/
if (!layout.codes.hasOwnProperty(l.code))
layout.codes[l.code] = l.code;
/*
* nice print of the layout
*/
l.toString = function(){return this.id};
/*
* reset hash, to be recalculated on options draw
*/
layout.options = null;
}
}
/**
* Set current layout
*
* @param {String} code layout name
* @return {Boolean} change state
* @scope public
*/
self.switchLayout = function (code) {
var res = (!lang || code != lang.toString());
if (res) {
/*
* trying to regenerate options list
*/
__buildOptionsList();
if (!code) {
code = nodes.langbox.value;
}
if (!layout.options.hasOwnProperty(code)) return false;
__setProgress(10);
/*
* hide IME on layout switch
*/
self.IME.hide();
/*
* touch the dropdown box
*/
nodes.langbox.options[layout.options[code]].selected = true;
lang = layout.hash[code];
res = !!lang;
if (res) {
/*
* trying to load resources before switching layout
*/
if (lang.requires) {
var arr = lang.requires.map(function(path){return gluePath(basePath,"/layouts/",path)});
var loading = lang.toString();
ScriptQueue.queue(arr, function() {
/*
* don't notify about script loading if layout was changed in the middle of loading
*/
if (lang.toString() == loading) {
__layoutLoadMonitor.apply(self, arguments);
}
});
} else {
__layoutLoadMonitor(null, true);
}
} else {
__layoutLoadMonitor(null, false);
}
} else {
res = lang && code == lang.toString();
// window.console.out = "Please wait, keyboard is not available";
}
return res;
}
/**
* Return the list of the available layouts
*
* @return {Array}
* @scope public
*/
self.getLayouts = function () {
var lts = [];
for (var i=0,lL=layout.length;i<lL;i++) {
lts[lts.length] = [layout[i].code,layout[i].name];
}
return lts.sort();
}
/**
* Sets the layouts groups, available for operations
* Accepts a serie of strings, supposed to be layout group names
*
* @scope public
*/
self.setVisibleLayoutCodes = function () {
var codes = isArray(arguments[0])?arguments[0]:arguments
,filter = null
,code;
for (var i in layout.codes) {
if (layout.codes.hasOwnProperty(i)) {
code = i.toUpperCase();
if (codes.indexOf(code) > -1) {
if (!filter)
filter = {};
filter[code] = code;
}
}
}
layout.codeFilter = filter;
/*
* reset hash, to be recalculated on options draw
*/
layout.options = null;
lang = null;
if (!self.switchLayout(nodes.langbox.value)) {
/*
* if first try fails, make a second try... on the regenerated list
*/
self.switchLayout(nodes.langbox.value);
}
}
/**
* Returns available layout codes
*
* @return {Array} codes
*/
self.getLayoutCodes = function () {
var codes = [];
for (var i in layout.codes) {
if (!layout.codes.hasOwnProperty(i))
continue;
codes.push(i);
}
return codes.sort();
}
//---------------------------------------------------------------------------
// GLOBAL EVENT HANDLERS
//---------------------------------------------------------------------------
/**
* Do the key clicks, caught from both virtual and real keyboards
*
* @param {HTMLInputElement} key on the virtual keyboard
* @param {EventTarget} evt optional event object, to be used to re-map the keyCode
* @scope private
*/
var _keyClicker_ = function (key, evt) {
var chr = ""
,ret = false;
key = key.replace(idPrefix, "");
switch (key) {
case KEY.CAPS :
case KEY.SHIFT :
case "shift_left" :
case "shift_right" :
case KEY.ALT :
case "alt_left" :
case "alt_right" :
return true;
case 'backspace':
/*
* if layout has char processor and there's any selection, ask it for advice
*/
if (isFunction(lang.charProcessor) && DocumentSelection.getSelection(nodes.attachedInput).length) {
chr = "\x08";
} else if (evt && evt.currentTarget == nodes.attachedInput) {
self.IME.hide(true);
return true;
} else {
DocumentSelection.deleteAtCursor(nodes.attachedInput, false);
self.IME.hide(true);
}
break;
case 'del':
self.IME.hide(true);
if (evt)
return true;
DocumentSelection.deleteAtCursor(nodes.attachedInput, true);
break;
case 'space':
chr = " ";
break;
case 'tab':
chr = "\t";
break;
case 'enter':
chr = "\n";
break;
default:
chr = lang.keys[key][mode];
break;
}
if (chr) {
/*
* process current selection and new symbol with __charProcessor, it might update them
*/
if (!(chr = __charProcessor(chr, DocumentSelection.getSelection(nodes.attachedInput)))) return ret;
/*
* try to create an event, then fallback to DocumentSelection, if something fails
*/
var virtualprint = false;
/*
* there are some global exceptions, when createEvent won't work properly
* - selection to set exists
* - multiple symbols should be inserted
* - multibyte unicode symbol should be inserted
* - rich text editor attached
* note, we will skip the default behavior of the TAB
*/
if (!chr[1]
&& chr[0].length<=1
&& chr[0].charCodeAt(0)<=0x7fff
&& !nodes.attachedInput.contentDocument
// && '\t' != chr[0]
) {
var ck = chr[0].charCodeAt(0);
virtualprint = !__fireKbdEvent(ck, evt);
} else {
virtualprint = true;
}
if (virtualprint) {
DocumentSelection.insertAtCursor(nodes.attachedInput,chr[0]);
/*
* select as much, as __charProcessor callback requested
*/
if (chr[1]) {
DocumentSelection.setRange(nodes.attachedInput,-chr[1],0,true);
}
}
}
return ret;
}
/**
* Captures some keyboard events
*
* @param {Event} keydown
* @scope protected
*/
var _keydownHandler_ = function(e) {
/*
* it's global event handler. do not process event, if keyboard is not ready to work
*/
if (!self.isEnabled() || !self.isOpen()) return;
/*
* record new keyboard mode
*/
var newMode = mode;
/*
* differently process different events
*/
var keyCode = e.getKeyCode();
switch (e.type) {
case 'keydown' :
switch (keyCode) {
case 9: // don't touch the default behavior of Tab key,
break;
case 37:
if (self.IME.isOpen()) {
self.IME.prevPage(e);
e.preventDefault();
}
break;
case 39:
if (self.IME.isOpen()) {
self.IME.nextPage(e);
e.preventDefault();
}
break;
case 38:
if (self.IME.isOpen()) {
if (!self.IME.showPaged())
self.IME.prevPage(e);
e.preventDefault();
}
break;
case 40:
if (self.IME.isOpen()) {
if (!self.IME.showAllPages())
self.IME.nextPage(e);
e.preventDefault();
}
break;
case 8: // backspace
case 46: // del
var el = nodes.desk.childNodes[keymap[keyCode]];
/*
* set the class only 1 time
*/
if (animate && !e.getRepeat()) DOM.CSS(el).addClass(cssClasses.buttonDown);
if (!_keyClicker_(el.id, e)) e.preventDefault();
break;
case 20: //caps lock
if (!e.getRepeat()) {
newMode = newMode ^ VK_CAPS;
}
break;
case 27:
if (self.IME.isOpen()) {
self.IME.hide();
} else {
var start = DocumentSelection.getStart(nodes.attachedInput);
DocumentSelection.setRange(nodes.attachedInput, start, start);
}
return false;
default:
if (!e.getRepeat()) {
newMode = newMode|e.shiftKey|e.ctrlKey<<2|e.altKey<<1;
}
if (keymap.hasOwnProperty(keyCode)) {
if (!(e.altKey ^ e.ctrlKey)) {
var el = nodes.desk.childNodes[keymap[keyCode]];
if (animate) DOM.CSS(el).addClass(cssClasses.buttonDown);
/*
* assign the key code to be inserted on the keypress
*/
newKeyCode = el.id;
}
if (e.altKey && e.ctrlKey) {
e.preventDefault();
/*
* this block is used to print a char when ctrl+alt pressed
* browsers does not invoke "kepress" in this case
*/
if (e.srcElement) {
_keyClicker_(nodes.desk.childNodes[keymap[keyCode]].id, e)
newKeyCode = "";
}
}
} else {
self.IME.hide();
}
break;
}
break;
case 'keyup' :
switch (keyCode) {
case 20:
break;
default:
if (!e.getRepeat()) {
newMode = mode&(VK_ALL^(!e.shiftKey|(!e.ctrlKey<<2)|(!e.altKey<<1)));
}
if (animate && keymap.hasOwnProperty(keyCode)) {
DOM.CSS(nodes.desk.childNodes[keymap[keyCode]]).removeClass(cssClasses.buttonDown);
}
}
break;
case 'keypress' :
/*
* flag is set only when virtual key passed to input target
*/
if (newKeyCode && !e.VK_bypass) {
if (!_keyClicker_(newKeyCode, e)) {
e.stopPropagation();
/*
* keeps browsers away from running built-in event handlers
*/
e.preventDefault();
}
/*
* reset flag
*/
newKeyCode = null;
}
if (!mode^VK_ALT_CTRL && (e.altKey || e.ctrlKey)) {
self.IME.hide();
}
if (0==keyCode && !newKeyCode && !e.VK_bypass // suppress dead keys from the keyboard driver
&& (!e.ctrlKey && !e.altKey && !e.shiftKey) // only when no special keys pressed, unblocking system shortcuts
) {
e.preventDefault();
}
}
/*
* update layout state
*/
if (newMode != mode) {
__updateControlKeys(newMode);
__updateLayout();
}
}
/**
* Handle clicks on the buttons, actually used with mouseup event
*
* @param {Event} mouseup event
* @scope protected
*/
var _btnClick_ = function (e) {
/*
* either a pressed key or something new
*/
var el = DOM.getParent(e.srcElement||e.target,'a');
/*
* skip invalid nodes
*/
if (!el || el.parentNode.id.indexOf(idPrefix)<0) return;
el = el.parentNode;
switch (el.id.substring(idPrefix.length)) {
case "caps":
case "shift_left":
case "shift_right":
case "alt_left":
case "alt_right":
case "ctrl_left":
case "ctrl_right":
return;
}
if (DOM.CSS(el).hasClass(cssClasses.buttonDown) || !animate) {
_keyClicker_(el.id);
}
if (animate) {
DOM.CSS(el).removeClass(cssClasses.buttonDown)
}
var newMode = mode&(VK_CAPS|e.shiftKey|e.altKey<<1|e.ctrlKey<<2);
if (mode != newMode) {
__updateControlKeys(newMode);
__updateLayout();
}
e.preventDefault();
e.stopPropagation();
}
/**
* Handle mousedown event
*
* Method is used to set 'pressed' button state and toggle shift, if needed
* Additionally, it is used by keyboard wrapper to forward keyboard events to the virtual keyboard
*
* @param {Event} mousedown event
* @scope protected
*/
var _btnMousedown_ = function (e) {
/*
* either pressed key or something new
*/
var el = DOM.getParent(e.srcElement||e.target, 'a');
/*
* skip invalid nodes
*/
if (!el || el.parentNode.id.indexOf(idPrefix)<0) return;
el = el.parentNode;
var newMode = mode;
var key = el.id.substring(idPrefix.length);
switch (key) {
case "caps":
newMode = newMode ^ VK_CAPS;
break;
case "shift_left":
case "shift_right":
/*
* Shift is pressed in on both keyboard and virtual keyboard, return
*/
if (e.shiftKey) break;
newMode = newMode ^ VK_SHIFT;
break;
case "alt_left":
case "alt_right":
case "ctrl_left":
case "ctrl_right":
newMode = newMode ^ (e.altKey<<1^VK_ALT) ^ (e.ctrlKey<<2^VK_CTRL);
break;
/*
* any real pressed key
*/
default:
if (animate) DOM.CSS(el).addClass(cssClasses.buttonDown)
break;
}
if (mode != newMode) {
__updateControlKeys(newMode);
__updateLayout();
}
e.preventDefault();
e.stopPropagation();
}
/**
* Handle mouseout and mouseover events
*
* Method is used to update button states, based on the focused button
*
* @param {Event} mouse event
* @scope protected
*/
var _btnMouseInOut_ = function (e) {
/*
* either pressed key or something new
*/
var el = DOM.getParent(e.srcElement||e.target, 'div')
,type = e.type=='mouseover'?2:/*'mouseout'*/3;
/*
* skip invalid nodes
*/
if (el && (id = el.id).indexOf(idPrefix)>-1) {
if (id.indexOf(KEY.SHIFT)>-1) {
/*
* both shift keys should be blurred
*/
__toggleControlKeysState(type, KEY.SHIFT);
} else if (id.indexOf(KEY.ALT)>-1 || id.indexOf(KEY.CTRL)>-1) {
/*
* both alt and ctrl keys should be blurred
*/
__toggleControlKeysState(type, KEY.CTRL);
__toggleControlKeysState(type, KEY.ALT);
} else if (id.indexOf(KEY.CAPS)>-1) {
/*
* CAPS should not loose it's 'down' state
*/
__toggleKeyState(type, el);
} else if (animate) {
__toggleKeyState(type, el);
if (3 == type) {
/*
* reset 'hover' state
*/
__toggleKeyState(0, el);
}
}
}
e.preventDefault();
e.stopPropagation();
}
/**
* Switches keyboard map...
*
* @param {Event} e
* @scope private
*/
var switchMapping = function (e) {
DocumentCookie.set('vk_mapping', e.target.value);
keymap = keymaps[e.target.value];
}
/**********************************************************
* MOST COMMON METHODS
**********************************************************/
/**
* Used to attach keyboard output to specified input
*
* @param {Null, HTMLInputElement,String} element to attach keyboard to
* @return {HTMLInputElement}
* @scope public
*/
self.attachInput = function (el) {
/*
* if null is supplied, don't change the target field
*/
if (!el) return nodes.attachedInput;
if (isString(el)) el = document.getElementById(el);
if (el == nodes.attachedInput || !el) return nodes.attachedInput;
/*
* perform initialization...
*/
if (!self.switchLayout(options.layout) && !self.switchLayout(nodes.langbox.value)) {
/*
* if both tries fail, go away
*/
throw new Error ('No layouts available');
}
/*
* detach everything
*/
self.detachInput();
if (!el || !el.tagName) {
nodes.attachedInput = null;
} else {
/*
* set keyboard animation for the current field
*/
animate = !DOM.CSS(el).hasClass(cssClasses.noanim);
/*
* for iframe target we track its HTML node
*/
nodes.attachedInput = el;
/*
* set input direction
*/
__toggleInputDir();
if (el.contentWindow) {
el = el.contentWindow.document.body.parentNode;
}
el.focus();
EM.addEventListener(el,'keydown',_keydownHandler_);
EM.addEventListener(el,'keyup',_keydownHandler_);
EM.addEventListener(el,'keypress',_keydownHandler_);
EM.addEventListener(el,'mousedown',self.IME.blurHandler);
/*
* if current input target does stay in some other document, bind
* events processing to the current document to keep key translation working
*/
var html = document.body.parentNode;
if (document.body.parentNode != DOM.getParent(el,'html')) {