forked from github/game-off-2013
-
Notifications
You must be signed in to change notification settings - Fork 1
/
keys.js
54 lines (47 loc) · 1.28 KB
/
keys.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
"use strict";
define([], function() {
document.addEventListener( 'keydown', onDocumentKeyDown, false );
document.addEventListener( 'keyup', onDocumentKeyUp, false );
var states = {};
var actions = {
UP: [87, 38],
LEFT: [65, 37],
DOWN: [83, 40],
RIGHT: [68, 39],
TOOL: [84],
};
function onDocumentKeyDown( event ) {
var keyCode = event.keyCode;
if( !states[keyCode] ) {
states[keyCode] = true;
onPress(keyCode);
}
}
function onDocumentKeyUp( event ) {
var keyCode = event.keyCode;
if( states[keyCode] ) {
states[keyCode] = false;
onRelease(keyCode);
}
}
function onPress(keyCode) {
function isKeycode(k) { return k === keyCode; }
for( var dir in actions ) {
if( actions[dir].some( isKeycode ) ) {
result.fireEvent(dir, true);
}
}
}
function onRelease(keyCode) {
function isKeycode(k) { return k === keyCode; }
for( var dir in actions ) {
if( actions[dir].some( isKeycode ) ) {
result.fireEvent(dir, false);
}
}
}
var result = {
fireEvent:undefined
};
return result;
});