OLD | NEW |
1 /* Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 /* Copyright (c) 2014 The Chromium Authors. All rights reserved. |
2 * Use of this source code is governed by a BSD-style license that can be | 2 * Use of this source code is governed by a BSD-style license that can be |
3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
4 */ | 4 */ |
5 | 5 |
6 /** | 6 /** |
7 * @constructor | 7 * @constructor |
8 * @param {HTMLElement} parentDiv | 8 * @param {HTMLElement} parentDiv |
9 */ | 9 */ |
10 var ChordTracker = function(parentDiv) { | 10 var ChordTracker = function(parentDiv) { |
| 11 /** @type {HTMLElement} */ |
11 this.parentDiv_ = parentDiv; | 12 this.parentDiv_ = parentDiv; |
| 13 /** @type {HTMLElement} */ |
12 this.currentDiv_ = null; | 14 this.currentDiv_ = null; |
| 15 /** @type {Object.<HTMLElement>} */ |
13 this.pressedKeys_ = {}; | 16 this.pressedKeys_ = {}; |
14 }; | 17 }; |
15 | 18 |
16 /** | 19 /** |
17 * @param {number} keyCode | 20 * @param {number|string} keyId Either a Javascript key-code, or a PNaCl "code" |
| 21 * string. |
18 * @param {string} title | 22 * @param {string} title |
19 * @return {void} | 23 * @return {void} |
20 */ | 24 */ |
21 ChordTracker.prototype.addKeyUpEvent = function(keyCode, title) { | 25 ChordTracker.prototype.addKeyUpEvent = function(keyId, title) { |
22 var text = this.keyName_(keyCode); | 26 var text = this.keyName_(keyId); |
23 var span = this.addSpanElement_('key-up', text, title); | 27 var span = this.addSpanElement_('key-up', text, title); |
24 delete this.pressedKeys_[keyCode]; | 28 delete this.pressedKeys_[keyId]; |
25 if (!this.keysPressed_()) { | 29 if (!this.keysPressed_()) { |
26 this.end_(); | 30 this.end_(); |
27 } | 31 } |
28 }; | 32 }; |
29 | 33 |
30 /** | 34 /** |
31 * @param {number} keyCode | 35 * @param {number|string} keyId Either a Javascript key-code, or a PNaCl "code" |
| 36 * string. |
32 * @param {string} title | 37 * @param {string} title |
33 * @return {void} | 38 * @return {void} |
34 */ | 39 */ |
35 ChordTracker.prototype.addKeyDownEvent = function(keyCode, title) { | 40 ChordTracker.prototype.addKeyDownEvent = function(keyId, title) { |
36 var text = this.keyName_(keyCode); | 41 var text = this.keyName_(keyId); |
37 var span = this.addSpanElement_('key-down', text, title); | 42 var span = this.addSpanElement_('key-down', text, title); |
38 this.pressedKeys_[keyCode] = span; | 43 this.pressedKeys_[keyId] = span; |
39 }; | 44 }; |
40 | 45 |
41 /** | 46 /** |
42 * @param {string} characterText | 47 * @param {string} characterText |
43 * @param {string} title | 48 * @param {string} title |
44 * @return {void} | 49 * @return {void} |
45 */ | 50 */ |
46 ChordTracker.prototype.addCharEvent = function(characterText, title) { | 51 ChordTracker.prototype.addCharEvent = function(characterText, title) { |
47 this.addSpanElement_('char-event', characterText, title); | 52 this.addSpanElement_('char-event', characterText, title); |
48 }; | 53 }; |
49 | 54 |
50 /** | 55 /** |
51 * @return {void} | 56 * @return {void} |
52 */ | 57 */ |
53 ChordTracker.prototype.releaseAllKeys = function() { | 58 ChordTracker.prototype.releaseAllKeys = function() { |
54 this.end_(); | 59 this.end_(); |
55 for (var i in this.pressedKeys_) { | 60 for (var i in this.pressedKeys_) { |
56 this.pressedKeys_[i].classList.add('unreleased'); | 61 this.pressedKeys_[i].classList.add('unreleased'); |
57 } | 62 } |
58 this.pressedKeys_ = {}; | 63 this.pressedKeys_ = {}; |
59 } | 64 } |
60 | 65 |
61 /** | 66 /** |
62 * @private | 67 * @private |
63 * @param {string} className | 68 * @param {string} className |
64 * @param {string} text | 69 * @param {string} text |
65 * @param {string} title | 70 * @param {string} title |
| 71 * @return {HTMLElement} |
66 */ | 72 */ |
67 ChordTracker.prototype.addSpanElement_ = function(className, text, title) { | 73 ChordTracker.prototype.addSpanElement_ = function(className, text, title) { |
68 this.begin_(); | 74 this.begin_(); |
69 var span = document.createElement('span'); | 75 var span = /** @type {HTMLElement} */ (document.createElement('span')); |
70 span.classList.add(className); | 76 span.classList.add(className); |
| 77 span.classList.add('key-div'); |
71 span.innerText = text; | 78 span.innerText = text; |
72 span.title = title; | 79 span.title = title; |
73 this.currentDiv_.appendChild(span); | 80 this.currentDiv_.appendChild(span); |
74 return span; | 81 return span; |
75 } | 82 } |
76 | 83 |
77 /** | 84 /** |
78 * @private | 85 * @private |
79 */ | 86 */ |
80 ChordTracker.prototype.begin_ = function() { | 87 ChordTracker.prototype.begin_ = function() { |
81 if (this.currentDiv_) { | 88 if (this.currentDiv_) { |
82 return; | 89 return; |
83 } | 90 } |
84 this.currentDiv_ = document.createElement('div'); | 91 this.currentDiv_ = /** @type {HTMLElement} */ (document.createElement('div')); |
85 this.currentDiv_.classList.add('chord-div'); | 92 this.currentDiv_.classList.add('chord-div'); |
86 this.parentDiv_.appendChild(this.currentDiv_); | 93 this.parentDiv_.appendChild(this.currentDiv_); |
87 }; | 94 }; |
88 | 95 |
89 /** | 96 /** |
90 * @private | 97 * @private |
91 */ | 98 */ |
92 ChordTracker.prototype.end_ = function() { | 99 ChordTracker.prototype.end_ = function() { |
93 if (!this.currentDiv_) { | 100 if (!this.currentDiv_) { |
94 return; | 101 return; |
(...skipping 11 matching lines...) Expand all Loading... |
106 * @private | 113 * @private |
107 */ | 114 */ |
108 ChordTracker.prototype.keysPressed_ = function() { | 115 ChordTracker.prototype.keysPressed_ = function() { |
109 for (var property in this.pressedKeys_) { | 116 for (var property in this.pressedKeys_) { |
110 return true; | 117 return true; |
111 } | 118 } |
112 return false; | 119 return false; |
113 }; | 120 }; |
114 | 121 |
115 /** | 122 /** |
116 * @param {number} keyCode The keyCode field of the keyup or keydown event. | 123 * @param {number|string} keyId Either a Javascript key-code, or a PNaCl "code" |
| 124 * string. |
117 * @return {string} A human-readable representation of the key. | 125 * @return {string} A human-readable representation of the key. |
118 * @private | 126 * @private |
119 */ | 127 */ |
120 ChordTracker.prototype.keyName_ = function(keyCode) { | 128 ChordTracker.prototype.keyName_ = function(keyId) { |
121 var result = keyboardMap[keyCode]; | 129 if (typeof keyId == 'string') { |
| 130 return keyId; |
| 131 } |
| 132 var result = keyboardMap[keyId]; |
122 if (!result) { | 133 if (!result) { |
123 result = '<' + keyCode + '>'; | 134 result = '<' + keyId + '>'; |
124 } | 135 } |
125 return result; | 136 return result; |
126 }; | 137 }; |
127 | |
OLD | NEW |