summaryrefslogtreecommitdiffstats
path: root/ui/keyboard/resources/elements/kb-keyboard.html
blob: 0f19ccd2a80736e5ee3245b2c1d7e6e78121e972 (plain)
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
<!--
  -- Copyright (c) 2013 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.
  -->

<element name="kb-keyboard" on-key-over="keyOver" on-key-up="keyUp"
    on-key-down="keyDown" attributes="keyset rows">
  <template>
    <content select="#{{keyset}}"></content>
  </template>
  <script>
    /**
     * The long-press delay in milliseconds before long-press handler is
     * invoked.
     * @type {number}
     */
    var LONGPRESS_DELAY_MSEC = 500;

    /**
     * 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;

    /**
     * The boolean to decide if keyboard should transit to upper case keyset
     * when spacebar is pressed. If a closing punctuation is followed by a
     * spacebar, keyboard should automatically transit to upper case.
     * @type {boolean}
     */
    var enterUpperOnSpace = false;

    /**
     * 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;
      }
    };

    Polymer.register(this, {
      lastPressedKey: null,
      voiceInput_: null,

      ready: function() {
        this.voiceInput_ = new VoiceInput(this);
      },

      /**
       * Handles key-down event that is sent by kb-key.
       * @param {CustomEvent} event The key-down event dispatched by kb-key.
       * @param {Object} detail The detail of pressed kb-key.
       */
      keyDown: function(event, detail) {
        var toKeyset = detail.toKeyset;
        if (this.lastPressedKey)
          this.lastPressedKey.classList.remove('active');
        this.lastPressedKey = event.target;
        this.lastPressedKey.classList.add('active');
        repeatKey.cancel();
        if (detail.repeat) {
          sendKey(detail.char);
          repeatKey.key = this.lastPressedKey;
          repeatKey.timer = setTimeout(function() {
            repeatKey.timer = undefined;
            repeatKey.interval = setInterval(function() {
               sendKey(detail.char);
            }, REPEAT_INTERVAL_MSEC);
          }, Math.max(0, REPEAT_DELAY_MSEC - REPEAT_INTERVAL_MSEC));
        }
      },

      /**
       * Handles key-up event that is sent by kb-key.
       * @param {CustomEvent} event The key-up event dispatched by kb-key.
       * @param {Object} detail The detail of pressed kb-key.
       */
      keyUp: function(event, detail) {
        this.lastPressedKey.classList.remove('active');
        if (this.lastPressedKey != event.target)
          return;
        if (repeatKey.key == event.target) {
          repeatKey.cancel();
          return;
        }
        var toKeyset = detail.toKeyset;
        // Keyset transtion key.
        if (toKeyset) {
          this.keyset = toKeyset;
        }
        var char = detail.char;
        if (enterUpperOnSpace) {
          enterUpperOnSpace = false;
          if (char == 'Spacebar')
            this.keyset = 'upper';
        }
        switch(char) {
          case 'Invalid':
            return;
          case 'Mic':
            this.voiceInput_.onDown();
            return;
          case '.':
          case '?':
          case '!':
            enterUpperOnSpace = true;
            break;
          case 'Tab':
          case 'Spacebar':
          case 'Enter':
            sendKey(char);
            return;
          default:
            break;
        }
        for (var i = 0; i < char.length; i++) {
          sendKey(char.charAt(i));
        }
      }
    });
  </script>
</element>