OLD | NEW |
---|---|
(Empty) | |
1 <!-- | |
2 -- Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
3 -- Use of this source code is governed by a BSD-style license that can be | |
4 -- found in the LICENSE file. | |
5 --> | |
6 | |
7 <element name="kb-keyboard" on-key-over="keyOver" on-key-up="keyUp" | |
8 on-key-down="keyDown" attributes="keyset rows"> | |
9 <template> | |
10 <content select="#{{keyset}}"></content> | |
11 </template> | |
12 <script> | |
13 /** | |
14 * The long-press delay in milliseconds before long-press handler is | |
15 * invoked. | |
16 * @type {number} | |
17 */ | |
18 var LONGPRESS_DELAY_MSEC = 500; | |
19 | |
20 /** | |
21 * The repeat delay in milliseconds before a key starts repeating. Use the | |
22 * same rate as Chromebook. | |
23 * (See chrome/browser/chromeos/language_preferences.cc) | |
24 * @type {number} | |
25 */ | |
26 var REPEAT_DELAY_MSEC = 500; | |
27 | |
28 /** | |
29 * The repeat interval or number of milliseconds between subsequent | |
30 * keypresses. Use the same rate as Chromebook. | |
31 * @type {number} | |
32 */ | |
33 var REPEAT_INTERVAL_MSEC = 50; | |
34 | |
35 /** | |
36 * The boolean to decide if keyboard should transit to upper case keyset | |
37 * when spacebar is pressed. If a closing punctuation is followed by a | |
38 * spacebar, keyboard should automatically transit to upper case. | |
39 * @type {boolean} | |
40 */ | |
41 var enterUpperOnSpace = false; | |
42 | |
43 /** | |
44 * A structure to track the currently repeating key on the keyboard. | |
45 */ | |
46 var repeatKey = { | |
47 /** | |
48 * The timer for the delay before repeating behaviour begins. | |
49 * @type {number|undefined} | |
50 */ | |
51 timer: undefined, | |
52 | |
53 /** | |
54 * The interval timer for issuing keypresses of a repeating key. | |
55 * @type {number|undefined} | |
56 */ | |
57 interval: undefined, | |
58 | |
59 /** | |
60 * The key which is currently repeating. | |
61 * @type {BaseKey|undefined} | |
62 */ | |
63 key: undefined, | |
64 | |
65 /** | |
66 * Cancel the repeat timers of the currently active key. | |
67 */ | |
68 cancel: function() { | |
69 clearTimeout(this.timer); | |
70 clearInterval(this.interval); | |
71 this.timer = undefined; | |
72 this.interval = undefined; | |
73 this.key = undefined; | |
74 } | |
75 }; | |
76 | |
77 Polymer.register(this, { | |
78 lastPressedKey: null, | |
79 voiceInput_: null, | |
80 | |
81 ready: function() { | |
82 this.voiceInput_ = new VoiceInput(this); | |
83 }, | |
84 | |
85 /** | |
86 * Handles key-down event that is sent by kb-key. | |
87 * @param {CustomEvent} event The key-down event dispatched by kb-key. | |
88 * @param {Object} detail The detail of pressed kb-key. | |
89 */ | |
90 keyDown: function(event, detail) { | |
91 var to = detail.to; | |
92 if (this.lastPressedKey) | |
93 this.lastPressedKey.classList.remove('active'); | |
94 this.lastPressedKey = event.target; | |
95 this.lastPressedKey.classList.add('active'); | |
96 repeatKey.cancel(); | |
97 if (detail.char == "Mic") { | |
bryeung
2013/05/23 21:39:28
is there any way we can push this out of the gener
bshe
2013/05/24 14:43:50
I imagine this would merge to the "to" below in th
| |
98 this.voiceInput_.onDown(); | |
99 return; | |
100 } | |
101 // Keyset transtion key. | |
102 if (to) { | |
bryeung
2013/05/23 21:39:28
can we rename "to" to something a bit more descrip
bshe
2013/05/24 14:43:50
renamed to "toKeyset", but I am not sure if this i
| |
103 this.keyset = to; | |
104 return; | |
105 } | |
106 if (detail.repeat) { | |
107 var char = detail.char; | |
108 var keyEvent = { | |
109 keyIdentifier: char | |
110 }; | |
111 sendKeyEvent(keyEvent); | |
112 repeatKey.key = this.lastPressedKey; | |
113 repeatKey.timer = setTimeout(function() { | |
114 repeatKey.timer = undefined; | |
115 repeatKey.interval = setInterval(function() { | |
116 sendKeyEvent(keyEvent); | |
117 }, REPEAT_INTERVAL_MSEC); | |
118 }, Math.max(0, REPEAT_DELAY_MSEC - REPEAT_INTERVAL_MSEC)); | |
119 } | |
120 }, | |
121 | |
122 /** | |
123 * Handles key-up event that is sent by kb-key. | |
124 * @param {CustomEvent} event The key-up event dispatched by kb-key. | |
125 * @param {Object} detail The detail of pressed kb-key. | |
126 */ | |
127 keyUp: function(event, detail) { | |
128 this.lastPressedKey.classList.remove('active'); | |
129 if (this.lastPressedKey != event.target) | |
130 return; | |
131 if (repeatKey.key == event.target) { | |
132 repeatKey.cancel(); | |
133 return; | |
134 } | |
135 var to = detail.to; | |
136 // Keyset transtion key. | |
137 if (to) { | |
138 this.keyset = to; | |
bryeung
2013/05/23 21:39:28
why are we doing this on up and down? I must not
bshe
2013/05/24 14:43:50
This is mainly for upper case keys. When an upper
bryeung
2013/05/24 19:33:47
I'm fine with transitioning only on keyUp. Let's
bshe
2013/05/29 16:35:01
Done. Please see the latest patchset, the main fil
| |
139 } | |
140 var char = detail.char; | |
141 if (enterUpperOnSpace) { | |
142 enterUpperOnSpace = false; | |
143 if (char == 'Spacebar') | |
144 this.keyset = 'upper'; | |
145 } | |
146 switch(char) { | |
147 case 'Shift': | |
148 case 'Symbol': | |
149 case 'More': | |
150 case 'Mic': | |
151 return; | |
152 case '.': | |
153 case '?': | |
154 case '!': | |
155 enterUpperOnSpace = true; | |
156 break; | |
157 case 'Tab': | |
158 case 'Spacebar': | |
159 case 'Enter': | |
160 sendKeyEvent({keyIdentifier: char}); | |
161 return; | |
162 default: | |
163 break; | |
164 } | |
165 for (var i = 0; i < char.length; i++) { | |
166 var keyEvent = { | |
167 keyIdentifier: char.charAt(i) | |
168 }; | |
169 sendKeyEvent(keyEvent); | |
170 } | |
171 } | |
172 }); | |
173 </script> | |
174 </element> | |
175 | |
OLD | NEW |