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
3 * found in the LICENSE file.
8 * @param {HTMLElement} parentDiv
10 var ChordTracker = function(parentDiv) {
11 /** @type {HTMLElement} */
12 this.parentDiv_ = parentDiv;
13 /** @type {HTMLElement} */
14 this.currentDiv_ = null;
15 /** @type {Object<HTMLElement>} */
16 this.pressedKeys_ = {};
20 * @param {string} code The PNaCl "code" string.
21 * @param {string} title
24 ChordTracker.prototype.addKeyUpEvent = function(code, title) {
25 var span = this.addSpanElement_('key-up', code, title);
26 delete this.pressedKeys_[code];
27 if (!this.keysPressed_()) {
33 * @param {string} code The PNaCl "code" string.
34 * @param {string} title
37 ChordTracker.prototype.addKeyDownEvent = function(code, title) {
38 var span = this.addSpanElement_('key-down', code, title);
39 this.pressedKeys_[code] = span;
43 * @param {string} characterText
44 * @param {string} title
47 ChordTracker.prototype.addCharEvent = function(characterText, title) {
48 this.addSpanElement_('char-event', characterText, title);
54 ChordTracker.prototype.releaseAllKeys = function() {
56 for (var i in this.pressedKeys_) {
57 this.pressedKeys_[i].classList.add('unreleased');
59 this.pressedKeys_ = {};
64 * @param {string} className
65 * @param {string} text
66 * @param {string} title
67 * @return {HTMLElement}
69 ChordTracker.prototype.addSpanElement_ = function(className, text, title) {
71 var span = /** @type {HTMLElement} */ (document.createElement('span'));
72 span.classList.add(className);
73 span.classList.add('key-div');
74 span.innerText = text;
76 this.currentDiv_.appendChild(span);
83 ChordTracker.prototype.begin_ = function() {
84 if (this.currentDiv_) {
87 this.currentDiv_ = /** @type {HTMLElement} */ (document.createElement('div'));
88 this.currentDiv_.classList.add('chord-div');
89 this.parentDiv_.appendChild(this.currentDiv_);
95 ChordTracker.prototype.end_ = function() {
96 if (!this.currentDiv_) {
99 if (this.keysPressed_()) {
100 this.currentDiv_.classList.add('some-keys-still-pressed');
102 this.currentDiv_.classList.add('all-keys-released');
104 this.currentDiv_ = null;
108 * @return {boolean} True if there are any keys pressed down.
111 ChordTracker.prototype.keysPressed_ = function() {
112 for (var property in this.pressedKeys_) {