-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.lua
More file actions
29 lines (23 loc) · 1 KB
/
Copy pathinput.lua
File metadata and controls
29 lines (23 loc) · 1 KB
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
local Input={}
local keyboardPressedLastFrame={}
local keyboardPressedThisFrame={}
function Input.update()
keyboardPressedLastFrame=keyboardPressedThisFrame
keyboardPressedThisFrame={}
end
function Input.keypressed(key, scancode, isrepeat)
if not isrepeat then
keyboardPressedThisFrame[key] = true
end
end
function Input.isKeyJustPressed(key)
-- Input.update() is called at the beginning of love.update, so must use keyboardPressedLastFrame instead of keyboardPressedThisFrame.
-- but why don't move Input.update to the end of love.update, then keyboardPressedThisFrame table isn't emptied when calling G.update, and we can discard lastFrame table?
-- if so, it's possible that during G.update love.keyboard.keypressed is called, then in same frame, isKeyJustPressed call can yield different result.
return keyboardPressedLastFrame[key]
end
-- clear pressed keys record, to achieve "block inputs" for notices
function Input.consume()
keyboardPressedLastFrame={}
end
return Input