1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 * The repeat delay in milliseconds before a key starts repeating. Use the
7 * same rate as Chromebook.
8 * (See chrome/browser/chromeos/language_preferences.cc)
12 var REPEAT_DELAY_MSEC = 500;
15 * The repeat interval or number of milliseconds between subsequent
16 * keypresses. Use the same rate as Chromebook.
20 var REPEAT_INTERVAL_MSEC = 50;
23 * The double click/tap interval.
27 var DBL_INTERVAL_MSEC = 300;
30 * The index of the name of the keyset when searching for all keysets.
34 var REGEX_KEYSET_INDEX = 1;
37 * The integer number of matches when searching for keysets.
41 var REGEX_MATCH_COUNT = 2;
44 * The boolean to decide if keyboard should transit to upper case keyset
45 * when spacebar is pressed. If a closing punctuation is followed by a
46 * spacebar, keyboard should automatically transit to upper case.
49 var enterUpperOnSpace = false;
52 * A structure to track the currently repeating key on the keyboard.
57 * The timer for the delay before repeating behaviour begins.
58 * @type {number|undefined}
63 * The interval timer for issuing keypresses of a repeating key.
64 * @type {number|undefined}
69 * The key which is currently repeating.
70 * @type {BaseKey|undefined}
75 * Cancel the repeat timers of the currently active key.
78 clearTimeout(this.timer);
79 clearInterval(this.interval);
80 this.timer = undefined;
81 this.interval = undefined;
87 * The minimum movement interval needed to trigger cursor move on
88 * horizontal and vertical way.
92 var MIN_SWIPE_DIST_X = 50;
93 var MIN_SWIPE_DIST_Y = 20;
96 * The maximum swipe distance that will trigger hintText of a key
101 var MAX_SWIPE_FLICK_DIST = 60;
104 * The boolean to decide if it is swipe in process or finished.
107 var swipeInProgress = false;
109 // Flag values for ctrl, alt and shift as defined by EventFlags
110 // in "event_constants.h".
120 * A structure to track the current swipe status.
124 * The latest PointerMove event in the swipe.
127 currentEvent: undefined,
130 * Whether or not a swipe changes direction.
136 * The count of horizontal and vertical movement.
143 * Last touch coordinate.
150 * The PointerMove event which triggered the swipe.
153 startEvent: undefined,
156 * The flag of current modifier key.
162 * Current swipe direction.
168 * The number of times we've swiped within a single swipe.
174 * Returns the combined direction of the x and y offsets.
175 * @return {number} The latest direction.
177 getOffsetDirection: function() {
178 // TODO (rsadam): Use angles to figure out the direction.
180 // Checks for horizontal swipe.
181 if (Math.abs(this.offset_x) > MIN_SWIPE_DIST_X) {
182 if (this.offset_x > 0) {
183 direction |= SwipeDirection.RIGHT;
185 direction |= SwipeDirection.LEFT;
188 // Checks for vertical swipe.
189 if (Math.abs(this.offset_y) > MIN_SWIPE_DIST_Y) {
190 if (this.offset_y < 0) {
191 direction |= SwipeDirection.UP;
193 direction |= SwipeDirection.DOWN;
200 * Populates the swipe update details.
201 * @param {boolean} endSwipe Whether this is the final event for this
203 * @return {Object} The current state of the swipeTracker.
205 populateDetails: function(endSwipe) {
207 detail.direction = this.swipeDirection;
208 detail.index = this.swipeIndex;
209 detail.status = this.swipeStatus;
210 detail.endSwipe = endSwipe;
211 detail.startEvent = this.startEvent;
212 detail.currentEvent = this.currentEvent;
213 detail.isComplex = this.isComplex;
218 * Reset all the values when swipe finished.
220 resetAll: function() {
226 this.swipeDirection = 0;
228 this.startEvent = undefined;
229 this.currentEvent = undefined;
230 this.isComplex = false;
234 * Updates the swipe path with the current event.
235 * @param {Object} event The PointerEvent that triggered this update.
236 * @return {boolean} Whether or not to notify swipe observers.
238 update: function(event) {
242 this.offset_x += event.screenX - this.pre_x;
243 this.offset_y += event.screenY - this.pre_y;
244 this.pre_x = event.screenX;
245 this.pre_y = event.screenY;
247 // Check if movement crosses minimum thresholds in each direction.
248 var direction = this.getOffsetDirection();
251 // If swipeIndex is zero the current event is triggering the swipe.
252 if (this.swipeIndex == 0) {
253 this.startEvent = event;
254 } else if (direction != this.swipeDirection) {
255 // Toggle the isComplex flag.
256 this.isComplex = true;
258 // Update the swipe tracker.
259 this.swipeDirection = direction;
262 this.currentEvent = event;
269 Polymer('kb-keyboard', {
276 lastPressedKey: null,
282 //TODO(rsadam@): Add a control to let users change this.
283 volume: DEFAULT_VOLUME,
286 * The default input type to keyboard layout map. The key must be one of
287 * the input box type values.
290 inputTypeToLayoutMap: {
297 * Caches the specified sound on the keyboard.
298 * @param {string} soundId The name of the .wav file in the "sounds"
301 addSound: function(soundId) {
302 // Check if already loaded.
303 if (soundId == Sound.NONE || this.sounds[soundId])
306 for (var i = 0; i < SOUND_POOL_SIZE; i++) {
307 var audio = document.createElement('audio');
308 audio.preload = "auto";
310 audio.src = "../sounds/" + soundId + ".wav";
311 audio.volume = this.volume;
314 this.sounds[soundId] = pool;
318 * Changes the current keyset.
319 * @param {Object} detail The detail of the event that called this
322 changeKeyset: function(detail) {
323 if (detail.relegateToShift && this.shift) {
324 this.keyset = this.shift.textKeyset;
325 this.activeKeyset.nextKeyset = undefined;
328 var toKeyset = detail.toKeyset;
330 this.keyset = toKeyset;
331 this.activeKeyset.nextKeyset = detail.nextKeyset;
337 keysetChanged: function() {
338 var keyset = this.activeKeyset;
339 // Show the keyset if it has been initialized.
344 configChanged: function() {
345 this.layout = this.config.layout;
349 this.voiceInput_ = new VoiceInput(this);
350 this.swipeHandler = this.move.bind(this);
352 getKeyboardConfig(function(config) {
353 self.config = config;
358 * Registers a callback for state change events.
359 * @param{!Function} callback Callback function to register.
361 addKeysetChangedObserver: function(callback) {
362 this.addEventListener('stateChange', callback);
366 * Called when the type of focused input box changes. If a keyboard layout
367 * is defined for the current input type, that layout will be loaded.
368 * Otherwise, the keyboard layout for 'text' type will be loaded.
370 inputTypeChanged: function() {
371 // Disable layout switching at accessbility mode.
372 if (this.config && this.config.a11ymode)
375 // TODO(bshe): Toggle visibility of some keys in a keyboard layout
376 // according to the input type.
377 var layout = this.inputTypeToLayoutMap[this.inputType];
379 layout = this.inputTypeToLayoutMap.text;
380 this.layout = layout;
384 * When double click/tap event is enabled, the second key-down and key-up
385 * events on the same key should be skipped. Return true when the event
386 * with |detail| should be skipped.
387 * @param {Object} detail The detail of key-up or key-down event.
389 skipEvent: function(detail) {
390 if (this.dblDetail_) {
391 if (this.dblDetail_.char != detail.char) {
392 // The second key down is not on the same key. Double click/tap
393 // should be ignored.
394 this.dblDetail_ = null;
395 clearTimeout(this.dblTimer_);
396 } else if (this.dblDetail_.clickCount == 1) {
404 * Handles a swipe update.
405 * param {Object} detail The swipe update details.
407 onSwipeUpdate: function(detail) {
408 var direction = detail.direction;
410 console.error("Swipe direction cannot be: " + direction);
411 // Triggers swipe editting if it's a purely horizontal swipe.
412 if (!(direction & (SwipeDirection.UP | SwipeDirection.DOWN))) {
413 // Nothing to do if the swipe has ended.
417 // TODO (rsadam): This doesn't take into account index shifts caused
418 // by vertical swipes.
419 if (detail.index % 2 != 0) {
420 modifiers |= Modifier.SHIFT;
421 modifiers |= Modifier.CONTROL;
423 MoveCursor(direction, modifiers);
426 // Triggers swipe hintText if it's a purely vertical swipe.
427 if (this.activeKeyset.flick &&
428 !(direction & (SwipeDirection.LEFT | SwipeDirection.RIGHT))) {
429 // Check if event is relevant to us.
430 if ((!detail.endSwipe) || (detail.isComplex))
433 var distance = Math.abs(detail.startEvent.screenY -
434 detail.currentEvent.screenY);
435 if (distance > MAX_SWIPE_FLICK_DIST)
437 var triggerKey = detail.startEvent.target;
438 if (triggerKey && triggerKey.onFlick)
439 triggerKey.onFlick(detail);
444 * This function is bound to swipeHandler. Updates the current swipe
445 * status so that PointerEvents can be converted to Swipe events.
446 * @param {PointerEvent} event.
448 move: function(event) {
449 if (!swipeTracker.update(event))
451 // Conversion was successful, swipe is now in progress.
452 swipeInProgress = true;
453 if (this.lastPressedKey) {
454 this.lastPressedKey.classList.remove('active');
455 this.lastPressedKey = null;
457 this.onSwipeUpdate(swipeTracker.populateDetails(false));
461 * Handles key-down event that is sent by kb-key-base.
462 * @param {CustomEvent} event The key-down event dispatched by
464 * @param {Object} detail The detail of pressed kb-key.
466 keyDown: function(event, detail) {
467 if (this.skipEvent(detail))
470 if (this.lastPressedKey) {
471 this.lastPressedKey.classList.remove('active');
472 this.lastPressedKey.autoRelease();
474 this.lastPressedKey = event.target;
475 this.lastPressedKey.classList.add('active');
477 this.playSound(detail.sound);
479 var char = detail.char;
482 this.classList.remove('caps-locked');
486 var modifier = char.toLowerCase() + "-active";
487 // Removes modifier if already active.
488 if (this.classList.contains(modifier))
489 this.classList.remove(modifier);
492 // Not all Invalid keys are transition keys. Reset control keys if
493 // we pressed a transition key.
494 if (event.target.toKeyset || detail.relegateToShift)
495 this.onNonControlKeyTyped();
500 this.shift.onNonControlKeyDown();
502 this.ctrl.onNonControlKeyDown();
504 this.alt.onNonControlKeyDown();
507 if(this.changeKeyset(detail))
510 this.keyTyped(detail);
511 this.onNonControlKeyTyped();
512 repeatKey.key = this.lastPressedKey;
514 repeatKey.timer = setTimeout(function() {
515 repeatKey.timer = undefined;
516 repeatKey.interval = setInterval(function() {
517 self.playSound(detail.sound);
518 self.keyTyped(detail);
519 }, REPEAT_INTERVAL_MSEC);
520 }, Math.max(0, REPEAT_DELAY_MSEC - REPEAT_INTERVAL_MSEC));
525 * Handles key-out event that is sent by kb-shift-key.
526 * @param {CustomEvent} event The key-out event dispatched by
528 * @param {Object} detail The detail of pressed kb-shift-key.
530 keyOut: function(event, detail) {
531 this.changeKeyset(detail);
535 * Enable/start double click/tap event recognition.
536 * @param {CustomEvent} event The enable-dbl event dispatched by
538 * @param {Object} detail The detail of pressed kb-shift-key.
540 enableDbl: function(event, detail) {
541 if (!this.dblDetail_) {
542 this.dblDetail_ = detail;
543 this.dblDetail_.clickCount = 0;
545 this.dblTimer_ = setTimeout(function() {
546 self.dblDetail_.callback = null;
547 self.dblDetail_ = null;
548 }, DBL_INTERVAL_MSEC);
553 * Enable the selection while swipe.
554 * @param {CustomEvent} event The enable-dbl event dispatched by
557 enableSel: function(event) {
558 // TODO(rsadam): Disabled for now. May come back if we revert swipe
559 // selection to not do word selection.
563 * Handles pointerdown event. This is used for swipe selection process.
564 * to get the start pre_x and pre_y. And also add a pointermove handler
565 * to start handling the swipe selection event.
566 * @param {PointerEvent} event The pointerup event that received by
569 down: function(event) {
570 var layout = getKeysetLayout(this.activeKeysetId);
571 var key = layout.findClosestKey(event.clientX, event.clientY);
574 if (event.isPrimary) {
575 swipeTracker.pre_x = event.screenX;
576 swipeTracker.pre_y = event.screenY;
577 this.addEventListener("pointermove", this.swipeHandler, false);
582 * Handles pointerup event. This is used for double tap/click events.
583 * @param {PointerEvent} event The pointerup event that bubbled to
586 up: function(event) {
587 var layout = getKeysetLayout(this.activeKeysetId);
588 var key = layout.findClosestKey(event.clientX, event.clientY);
591 // When touch typing, it is very possible that finger moves slightly out
592 // of the key area before releases. The key should not be dropped in
594 // TODO(rsadam@) Change behaviour such that the key drops and the second
596 if (this.lastPressedKey &&
597 this.lastPressedKey.pointerId == event.pointerId) {
598 this.lastPressedKey.autoRelease();
601 if (this.dblDetail_) {
602 this.dblDetail_.clickCount++;
603 if (this.dblDetail_.clickCount == 2) {
604 this.dblDetail_.callback();
605 this.changeKeyset(this.dblDetail_);
606 clearTimeout(this.dblTimer_);
608 this.classList.add('caps-locked');
610 this.dblDetail_ = null;
614 // TODO(zyaozhujun): There are some edge cases to deal with later.
615 // (for instance, what if a second finger trigger a down and up
616 // event sequence while swiping).
617 // When pointer up from the screen, a swipe selection session finished,
618 // all the data should be reset to prepare for the next session.
619 if (event.isPrimary && swipeInProgress) {
620 swipeInProgress = false;
621 this.onSwipeUpdate(swipeTracker.populateDetails(true))
622 swipeTracker.resetAll();
624 this.removeEventListener('pointermove', this.swipeHandler, false);
628 * Handles PointerOut event. This is used for when a swipe gesture goes
629 * outside of the keyboard window.
630 * @param {Object} event The pointerout event that bubbled to the
633 out: function(event) {
635 // Ignore if triggered from one of the keys.
636 if (this.compareDocumentPosition(event.relatedTarget) &
637 Node.DOCUMENT_POSITION_CONTAINED_BY)
640 this.onSwipeUpdate(swipeTracker.populateDetails(true))
641 // Touched outside of the keyboard area, so disables swipe.
642 swipeInProgress = false;
643 swipeTracker.resetAll();
644 this.removeEventListener('pointermove', this.swipeHandler, false);
648 * Handles a TypeKey event. This is used for when we programmatically
649 * want to type a specific key.
650 * @param {CustomEvent} event The TypeKey event that bubbled to the
653 type: function(event) {
654 this.keyTyped(event.detail);
658 * Handles key-up event that is sent by kb-key-base.
659 * @param {CustomEvent} event The key-up event dispatched by kb-key-base.
660 * @param {Object} detail The detail of pressed kb-key.
662 keyUp: function(event, detail) {
663 if (this.skipEvent(detail))
667 if (detail.activeModifier) {
668 var modifier = detail.activeModifier.toLowerCase() + "-active";
669 this.classList.add(modifier);
671 // Adds the current keyboard modifiers to the detail.
673 detail.controlModifier = this.ctrl.isActive();
675 detail.altModifier = this.alt.isActive();
676 if (this.lastPressedKey)
677 this.lastPressedKey.classList.remove('active');
678 // Keyset transition key. This is needed to transition from upper
679 // to lower case when we are not in caps mode, as well as when
680 // we're ending chording.
681 this.changeKeyset(detail);
683 if (this.lastPressedKey &&
684 this.lastPressedKey.charValue != event.target.charValue) {
687 if (repeatKey.key == event.target) {
689 this.lastPressedKey = null;
692 var toLayoutId = detail.toLayout;
693 // Layout transition key.
695 this.layout = toLayoutId;
696 var char = detail.char;
697 this.lastPressedKey = null;
698 // Characters that should not be typed.
704 enterUpperOnSpace = false;
705 swipeTracker.swipeFlags = 0;
708 this.voiceInput_.onDown();
713 // Tries to type the character. Resorts to insertText if that fails.
714 if(!this.keyTyped(detail))
716 // Post-typing logic.
720 if(enterUpperOnSpace) {
721 enterUpperOnSpace = false;
723 var shiftDetail = this.shift.onSpaceAfterPunctuation();
724 // Check if transition defined.
725 this.changeKeyset(shiftDetail);
727 console.error('Capitalization on space after punctuation \
728 enabled, but cannot find target keyset.');
730 // Immediately return to maintain shift-state. Space is a
731 // non-control key and would otherwise trigger a reset of the
732 // shift key, causing a transition to lower case.
739 enterUpperOnSpace = this.shouldUpperOnSpace();
742 enterUpperOnSpace = false;
745 // Reset control keys.
746 this.onNonControlKeyTyped();
750 * Handles key-longpress event that is sent by kb-key-base.
751 * @param {CustomEvent} event The key-longpress event dispatched by
753 * @param {Object} detail The detail of pressed key.
755 keyLongpress: function(event, detail) {
756 // If the gesture is long press, remove the pointermove listener.
757 this.removeEventListener('pointermove', this.swipeHandler, false);
758 // Keyset transtion key.
759 if (this.changeKeyset(detail)) {
760 // Locks the keyset before removing active to prevent flicker.
761 this.classList.add('caps-locked');
762 // Makes last pressed key inactive if transit to a new keyset on long
764 if (this.lastPressedKey)
765 this.lastPressedKey.classList.remove('active');
770 * Plays the specified sound.
771 * @param {Sound} sound The id of the audio tag.
773 playSound: function(sound) {
774 if (!SOUND_ENABLED || !sound || sound == Sound.NONE)
776 var pool = this.sounds[sound];
778 console.error("Cannot find audio tag: " + sound);
781 // Search the sound pool for a free resource.
782 for (var i = 0; i < pool.length; i++) {
783 if (pool[i].paused) {
791 * Whether we should transit to upper case when seeing a space after
795 shouldUpperOnSpace: function() {
796 // TODO(rsadam): Add other input types in which we should not
797 // transition to upper after a space.
798 return this.inputTypeValue != 'password';
802 * Handler for the 'set-layout' event.
803 * @param {!Event} event The triggering event.
804 * @param {{layout: string}} details Details of the event, which contains
805 * the name of the layout to activate.
807 setLayout: function(event, details) {
808 this.layout = details.layout;
812 * Handles a change in the keyboard layout. Auto-selects the default
813 * keyset for the new layout.
815 layoutChanged: function() {
817 if (!this.selectDefaultKeyset()) {
818 console.error('No default keyset found for layout: ' + this.layout);
821 this.activeKeyset.show();
825 * Notifies the modifier keys that a non-control key was typed. This
826 * lets them reset sticky behaviour. A non-control key is defined as
827 * any key that is not Control, Alt, or Shift.
829 onNonControlKeyTyped: function() {
831 this.shift.onNonControlKeyTyped();
833 this.ctrl.onNonControlKeyTyped();
835 this.alt.onNonControlKeyTyped();
836 this.classList.remove('ctrl-active');
837 this.classList.remove('alt-active');
841 * Callback function for when volume is changed.
843 volumeChanged: function() {
844 var toChange = Object.keys(this.sounds);
845 for (var i = 0; i < toChange.length; i++) {
846 var pool = this.sounds[toChange[i]];
847 for (var j = 0; j < pool.length; j++) {
848 pool[j].volume = this.volume;
854 * Id for the active keyset.
857 get activeKeysetId() {
858 return this.layout + '-' + this.keyset;
862 * The active keyset DOM object.
866 return this.querySelector('#' + this.activeKeysetId);
870 * The current input type.
873 get inputTypeValue() {
874 return this.inputType;
878 * Changes the input type if it's different from the current
879 * type, else resets the keyset to the default keyset.
882 set inputTypeValue(value) {
883 if (value == this.inputType)
884 this.selectDefaultKeyset();
886 this.inputType = value;
890 * The keyboard is ready for input once the target keyset appears
891 * in the distributed nodes for the keyboard.
892 * @return {boolean} Indicates if the keyboard is ready for input.
894 isReady: function() {
895 var keyset = this.activeKeyset;
898 var nodes = this.$.content.getDistributedNodes();
899 for (var i = 0; i < nodes.length; i++) {
900 if (nodes[i].id && nodes[i].id == keyset.id)
907 * Generates fabricated key events to simulate typing on a
909 * @param {Object} detail Attributes of the key being typed.
910 * @return {boolean} Whether the key type succeeded.
912 keyTyped: function(detail) {
913 var builder = this.$.keyCodeMetadata;
915 detail.controlModifier = this.ctrl.isActive();
917 detail.altModifier = this.alt.isActive();
918 var downEvent = builder.createVirtualKeyEvent(detail, "keydown");
920 sendKeyEvent(downEvent);
921 sendKeyEvent(builder.createVirtualKeyEvent(detail, "keyup"));
928 * Selects the default keyset for a layout.
929 * @return {boolean} True if successful. This method can fail if the
930 * keysets corresponding to the layout have not been injected.
932 selectDefaultKeyset: function() {
933 var keysets = this.querySelectorAll('kb-keyset');
934 // Full name of the keyset is of the form 'layout-keyset'.
935 var regex = new RegExp('^' + this.layout + '-(.+)');
936 var keysetsLoaded = false;
937 for (var i = 0; i < keysets.length; i++) {
938 var matches = keysets[i].id.match(regex);
939 if (matches && matches.length == REGEX_MATCH_COUNT) {
940 keysetsLoaded = true;
941 // Without both tests for a default keyset, it is possible to get
942 // into a state where multiple layouts are displayed. A
943 // reproducable test case is do the following set of keyset
944 // transitions: qwerty -> system -> dvorak -> qwerty.
945 // TODO(kevers): Investigate why this is the case.
946 if (keysets[i].isDefault ||
947 keysets[i].getAttribute('isDefault') == 'true') {
948 this.keyset = matches[REGEX_KEYSET_INDEX];
949 this.classList.remove('caps-locked');
950 this.classList.remove('alt-active');
951 this.classList.remove('ctrl-active');
953 this.shift = this.querySelector('kb-shift-key');
956 // Caches control key.
957 this.ctrl = this.querySelector('kb-modifier-key[char=Ctrl]');
961 this.alt = this.querySelector('kb-modifier-key[char=Alt]');
964 this.fire('stateChange', {
965 state: 'keysetLoaded',
974 console.error('No default keyset found for ' + this.layout);