10
-- This is literally what is being typed during the current frame.
11
-- e.g. if the user holds the shift key and presses the 'a' key,
12
-- this will be set to 'A'. Consult <allPressed()> if you
13
-- want to know what specific keys are being pressed.
17
-- private property: _thisFrame
18
-- what keys are pressed this frame
19
-- if you are interested in this, use allPressed() instead
23
-- private property: _lastFrame
24
-- what keys were pressed last frame
28
new = function (self, obj)
29
obj = self:extend(obj)
31
love.keypressed = function (key, unicode) obj:keyPressed(key, unicode) end
32
love.keyreleased = function (key, unicode) obj:keyReleased(key, unicode) end
33
if obj.onNew then obj:onNew() end
38
-- Are *any* of the keys passed held down this frame?
41
-- string key descriptions passed as individual arguments
46
pressed = function (self, ...)
48
for _, value in pairs(keys) do
50
assert(type(value) == 'string', 'all keys are strings; asked to check a ' .. type(value))
53
if self._thisFrame[value] then
61
-- Method: justPressed
62
-- Are *any* of the keys passed pressed for the first time this frame?
65
-- string key descriptions passed as individual arguments
70
justPressed = function (self, ...)
73
for _, value in pairs(keys) do
75
assert(type(value) == 'string', 'all keys are strings; asked to check a ' .. type(value))
78
if self._thisFrame[value] and not self._lastFrame[value] then
87
-- Are *all* of the keys passed not held down this frame?
90
-- string key descriptions passed as individual arguments
95
released = function (self, ...)
98
for _, value in pairs(keys) do
100
assert(type(value) == 'string', 'all keys are strings; asked to check a ' .. type(value))
103
if self._thisFrame[value] then
111
-- Method: justReleased
112
-- Are *any* of the keys passed released after being held last frame?
115
-- string key descriptions passed as individual arguments
120
justReleased = function (self, ...)
123
for _, value in pairs(keys) do
125
assert(type(value) == 'string', 'all keys are strings; asked to check a ' .. type(value))
128
if self._lastFrame[value] and not self._thisFrame[value] then
136
-- Method: allPressed
137
-- Returns all buttons currently pressed this frame.
143
-- string key descriptions; if nothing is pressed, nil
145
allPressed = function (self)
148
for key, value in pairs(self._thisFrame) do
149
if value then table.insert(result, key) end
152
return unpack(result)
155
-- Method: allJustPressed
156
-- Returns all keys just pressed this frame.
162
-- string key descriptions; if nothing is just pressed, nil
164
allJustPressed = function (self)
167
for key, value in pairs(self._thisFrame) do
168
if value and not self._lastFrame[key] then table.insert(result, key) end
171
return unpack(result)
174
-- Method: allJustReleased
175
-- Returns all keys just released this frame.
181
-- string key descriptions; if nothing is just pressed, nil
183
allJustReleased = function (self)
186
for key, value in pairs(self._thisFrame) do
187
if not value and self._lastFrame[key] then table.insert(result, key) end
190
return unpack(result)
193
-- Connects to the love.keypressed callback
195
keyPressed = function (self, key, unicode)
196
self._thisFrame[key] = true
197
if unicode and unicode >= 0x20 and unicode ~= 127 and unicode < 0x3000 then
198
self.typed = self.typed .. string.char(unicode)
201
-- aliases for modifiers
203
if key == 'rshift' or key == 'lshift' or
204
key == 'rctrl' or key == 'lctrl' or
205
key == 'ralt' or key == 'lalt' or
206
key == 'rmeta' or key == 'lmeta' or
207
key == 'rsuper' or key == 'lsuper' then
208
self._thisFrame[string.sub(key, 2)] = true
212
-- Connects to the love.keyreleased callback
214
keyReleased = function (self, key, unicode)
215
self._thisFrame[key] = false
217
-- aliases for modifiers
219
if key == 'rshift' or key == 'lshift' or
220
key == 'rctrl' or key == 'lctrl' or
221
key == 'ralt' or key == 'lalt' or
222
key == 'rmeta' or key == 'lmeta' or
223
key == 'rsuper' or key == 'lsuper' then
224
self._thisFrame[string.sub(key, 2)] = false
228
endFrame = function (self, elapsed)
229
for key, value in pairs(self._thisFrame) do
230
self._lastFrame[key] = value
234
Sprite.endFrame(self, elapsed)
237
update = function() end