BlueGit

timfa frameworkv3

Files

frameworkv3 / input.js

input.js

input.js 1.4 KiB Download Raw
1//<DOC> category GameInput
2
3var GameInput =
4{
5 g_keyStatus: [],
6 g_pKeyStatus: [],
7
8 g_pressKeyStatus: [],
9 g_ppressKeyStatus: [],
10
11 g_pressBlacklist: [],
12
13 g_init: function()
14 {
15 document.onkeydown = GameInput.g_setKeyStatus;
16 document.onkeyup = GameInput.g_setKeyStatus;
17 },
18
19 g_setKeyStatus: function(e)
20 {
21 GameInput.g_keyStatus[e.which] = (e.type == "keydown");
22
23 if(!GameInput.g_pressBlacklist[e.which])
24 GameInput.g_pressKeyStatus[e.which] = (e.type == "keydown");
25
26 GameInput.g_pressBlacklist[e.which] = (e.type == "keydown"); //only allow single presses if key has been released once
27 },
28
29 g_updateKeys: function()
30 {
31 GameInput.g_pKeyStatus = GameInput.g_keyStatus;
32 GameInput.g_ppressKeyStatus = GameInput.g_pressKeyStatus;
33 GameInput.g_pressKeyStatus = [];
34 },
35
36 //<DOC> <function> GameInput.IsPressed(keyCode)<br><br>Returns true if the key with given code 'keyCode' was pressed this frame. Only returns true the frame the key is pressed once until the key is released and pressed again.
37 isPressed: function(keyCode)
38 {
39 if(GameInput.g_ppressKeyStatus[keyCode] == null)
40 return false;
41 else
42 return GameInput.g_ppressKeyStatus[keyCode];
43 },
44
45 //<DOC> <function> GameInput.isHeld(keyCode)<br><br>returns true if the key with given code 'keyCode' is currently pressed.
46 isHeld: function(keyCode)
47 {
48 if(GameInput.g_pKeyStatus[keyCode] == null)
49 return false;
50 else
51 return GameInput.g_pKeyStatus[keyCode];
52 }
53}//