Debian Codegraph @

dokidoki/components/keyboard.lua

raw symbols SCIP
1 --- dokidoki.components.keyboard
2 --- ============================
3 ---
4 --- Provides keyboard-input related information for the game.
5 ---
6 --- Currently keys are identified by their GLFW key constants. For ascii
7 --- characters this is the character code, with letters always using the
8 --- upper-case value.
9
10 --- Implementation
11 --- --------------
12
13 local base = require 'dokidoki.base'
14
15 local key_states = {}
16 local old_key_states = {}
17
18 --- ### `key_pressed(key)`
19 --- Returns true if `key` was pressed since the last frame, false otherwise.
20 function key_pressed(key)
21 return not not (key_states[key] and not old_key_states[key])
22 end
23
24 --- ### `key_held(key)`
25 --- Returns true if `key` is currently down, false otherwise.
26 function key_held(key)
27 return not not key_states[key]
28 end
29
30 --- ### `key_released(key)`
31 --- Returns true if `key` was released since the last frame, false otherwise.
32 function key_released(key)
33 return not not (not key_states[key] and old_key_states[key])
34 end
35
36 game.actors.new_generic('key_monitor', function ()
37 function update_cleanup()
38 old_key_states = base.copy(key_states)
39 end
40 function handle_event(event)
41 if event.type == 'key' then
42 key_states[event.key] = event.is_down or nil
43 end
44 end
45 end)