-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyboard.js
50 lines (47 loc) · 879 Bytes
/
keyboard.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
function Keyboard() {
this.handler = 0;
}
Keyboard.prototype.onKeyDown = function(key, tetromino) {
switch(key) {
case 38:
//up
tetromino.rotate();
console.log("rotate");
return 0;
case 37:
//left
tetromino.left();
console.log("left");
return 0;
case 39:
// right
tetromino.right();
console.log("right");
return 0;
case 40:
//down
tetromino.down();
console.log("down");
return 0;
case 32:
// space: drop
tetromino.drop();
console.log("drop");
return 1;
case 80:
// p: pause
console.log("pause");
return 2;
case 67:
// c: hold
console.log("hold");
return 3;
}
};
Keyboard.prototype.init = function(handler) {
this.handler = handler;
document.addEventListener('keydown', this.handler);
};
Keyboard.prototype.remove = function(){
document.removeEventListener('keydown', this.handler);
};