forked from omnikrll/convex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeyboardController.js
54 lines (42 loc) · 1.53 KB
/
KeyboardController.js
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
var KeyboardController = (function() {
var KeyboardController = function() {
this.pressedKey = -1;
this.octave = 0;
this.noteKeys = [65, 87, 83, 69, 68, 70, 84, 71, 89, 72, 85, 74, 75];
this.sysKeys = [16, 17, 18, 91];
this.prevCode = -1;
}
KeyboardController.prototype.constructor = KeyboardController;
KeyboardController.prototype.baseKeystrokeHandler = function(e) {
var self = this,
pressedKey = self.pressedKey,
code = e.keyCode || e.which,
oldKey = document.getElementsByClassName('pressed');
if (self.noteKeys.indexOf(code) == -1 || self.sysKeys.indexOf(code) == -1) return;
if (pressedKey == code || code == 32) {
if (code == 32 && pressedKey != -1 && oldKey.length) oldKey[0].classList.remove('pressed');
pressedKey = -1;
self.noteOffHandler();
return;
}
if (code == 188 || code == 190) {
var inc = (code == 188) ? -12 : 12,
dir = (inc == -12) ? 'down' : 'up';
octave += inc;
document.getElementById('octave-val').innerHTML = octave/12 + 4;
return;
}
pressedKey == code;
self.noteOnHandler(code);
document.getElementById('ascii_' + code).classList.add('pressed');
self.prevCode = code;
}
// these would probably be different in different applications-
// should they just get defined within that application, like after the object has been instantiated?
KeyboardController.prototype.noteOnHandler = function(code) {
// handle note on action
}
KeyboardController.prototype.noteOffHandler = function() {
// handle note off action
}
})();