-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeyboardMonitor.hx
46 lines (37 loc) · 1013 Bytes
/
KeyboardMonitor.hx
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
import flash.ui.Keyboard;
import flash.display.Stage;
import flash.events.KeyboardEvent;
import flash.events.Event;
class KeyboardMonitor {
private var keys : Array<Bool>;
public function new() {
keys = new Array();
for (i in 0...256) {
keys[i] = false;
}
}
public function init(stage : Stage) {
stage.addEventListener(KeyboardEvent.KEY_DOWN, this.keypress);
stage.addEventListener(KeyboardEvent.KEY_UP, this.keyunpress);
stage.addEventListener(Event.DEACTIVATE, clearKeys);
}
private function keypress(event) {
keys[event.keyCode] = true;
}
private function keyunpress(event) {
keys[event.keyCode] = false;
}
private function clearKeys(event) {
for (i in 0...256) {
keys[i] = false;
}
}
public function clear() {
for (i in 0...256) {
keys[i] = false;
}
}
public function isDown(n) {
return keys[n];
}
}