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 this.parentDiv_ = parentDiv; | 11 this.parentDiv_ = parentDiv; |
12 this.currentDiv_ = null; | 12 this.currentDiv_ = null; |
13 this.pressedKeys_ = {}; | 13 this.pressedKeys_ = {}; |
14 }; | 14 }; |
15 | 15 |
16 /** | 16 /** |
17 * @param {Event} event The keyup or keydown event. | 17 * @param {number} keyCode |
18 * @param {string} title | |
19 * @return {void} | |
18 */ | 20 */ |
19 ChordTracker.prototype.addKeyEvent = function(event) { | 21 ChordTracker.prototype.addKeyUpEvent = function(keyCode, title) { |
20 this.begin_(); | 22 var text = this.keyName_(keyCode); |
21 var span = document.createElement('span'); | 23 var span = this.addSpanElement_('key-up', text, title); |
22 span.title = this.makeTitle_(event); | 24 delete this.pressedKeys_[keyCode]; |
23 if (event.type == 'keydown') { | 25 this.endIfNoKeysPressed_(); |
24 span.classList.add('key-down'); | |
25 this.pressedKeys_[event.keyCode] = span; | |
26 } else { | |
27 span.classList.add('key-up'); | |
28 delete this.pressedKeys_[event.keyCode]; | |
29 } | |
30 span.innerText = this.keyName_(event.keyCode); | |
31 this.currentDiv_.appendChild(span); | |
32 if (!this.keysPressed_()) { | |
33 this.end_(); | |
34 } | |
35 }; | 26 }; |
36 | 27 |
28 /** | |
29 * @param {number} keyCode | |
30 * @param {string} title | |
31 * @return {void} | |
32 */ | |
33 ChordTracker.prototype.addKeyDownEvent = function(keyCode, title) { | |
34 var text = this.keyName_(keyCode); | |
35 var span = this.addSpanElement_('key-down', text, title); | |
36 this.pressedKeys_[keyCode] = span; | |
37 this.endIfNoKeysPressed_(); | |
Jamie
2015/01/30 23:25:09
I don't think you need this here: a keydown event
Łukasz Anforowicz
2015/01/31 00:44:49
Done.
| |
38 }; | |
39 | |
40 /** | |
41 * @param {string} characterText | |
42 * @param {string} title | |
43 * @return {void} | |
44 */ | |
45 ChordTracker.prototype.addCharEvent = function(characterText, title) { | |
46 this.addSpanElement_('char-event', characterText, title); | |
47 }; | |
48 | |
49 /** | |
50 * @return {void} | |
51 */ | |
37 ChordTracker.prototype.releaseAllKeys = function() { | 52 ChordTracker.prototype.releaseAllKeys = function() { |
38 this.end_(); | 53 this.end_(); |
39 for (var i in this.pressedKeys_) { | 54 for (var i in this.pressedKeys_) { |
40 this.pressedKeys_[i].classList.add('unreleased'); | 55 this.pressedKeys_[i].classList.add('unreleased'); |
41 } | 56 } |
42 this.pressedKeys_ = {}; | 57 this.pressedKeys_ = {}; |
43 } | 58 } |
44 | 59 |
45 /** | 60 /** |
46 * @private | 61 * @private |
62 * @param {string} className | |
63 * @param {string} text | |
64 * @param {string} title | |
65 */ | |
66 ChordTracker.prototype.addSpanElement_ = function(className, text, title) { | |
67 this.begin_(); | |
68 var span = document.createElement('span'); | |
69 span.classList.add(className); | |
70 span.innerText = text; | |
71 span.title = title; | |
72 this.currentDiv_.appendChild(span); | |
73 return span; | |
74 } | |
75 | |
76 /** | |
77 * @private | |
78 */ | |
79 ChordTracker.prototype.endIfNoKeysPressed_ = function() { | |
80 if (!this.keysPressed_()) { | |
81 this.end_(); | |
82 } | |
83 } | |
84 | |
85 /** | |
86 * @private | |
47 */ | 87 */ |
48 ChordTracker.prototype.begin_ = function() { | 88 ChordTracker.prototype.begin_ = function() { |
49 if (this.currentDiv_) { | 89 if (this.currentDiv_) { |
50 return; | 90 return; |
51 } | 91 } |
52 this.currentDiv_ = document.createElement('div'); | 92 this.currentDiv_ = document.createElement('div'); |
53 this.currentDiv_.classList.add('chord-div'); | 93 this.currentDiv_.classList.add('chord-div'); |
54 this.parentDiv_.appendChild(this.currentDiv_); | 94 this.parentDiv_.appendChild(this.currentDiv_); |
55 }; | 95 }; |
56 | 96 |
(...skipping 29 matching lines...) Expand all Loading... | |
86 * @private | 126 * @private |
87 */ | 127 */ |
88 ChordTracker.prototype.keyName_ = function(keyCode) { | 128 ChordTracker.prototype.keyName_ = function(keyCode) { |
89 var result = keyboardMap[keyCode]; | 129 var result = keyboardMap[keyCode]; |
90 if (!result) { | 130 if (!result) { |
91 result = '<' + keyCode + '>'; | 131 result = '<' + keyCode + '>'; |
92 } | 132 } |
93 return result; | 133 return result; |
94 }; | 134 }; |
95 | 135 |
96 /** | |
97 * @param {Event} event The keyup or keydown event. | |
98 * @private | |
99 */ | |
100 ChordTracker.prototype.makeTitle_ = function(event) { | |
101 return 'type: ' + event.type + '\n' + | |
102 'alt: ' + event.altKey + '\n' + | |
103 'shift: ' + event.shiftKey + '\n' + | |
104 'control: ' + event.controlKey + '\n' + | |
105 'meta: ' + event.metaKey + '\n' + | |
106 'charCode: ' + event.charCode + '\n' + | |
107 'keyCode: ' + event.keyCode + '\n' + | |
108 'keyIdentifier: ' + event.keyIdentifier + '\n' + | |
109 'repeat: ' + event.repeat + '\n'; | |
110 }; | |
OLD | NEW |