11
-- This is literally what is being typed during the current frame.
12
-- e.g. if the user holds the shift key and presses the 'a' key,
13
-- this will be set to 'A'. Consult <allPressed()> if you
14
-- want to know what specific keys are being pressed.
18
-- private property: _thisFrame
19
-- what keys are pressed this frame
20
-- if you are interested in this, use allPressed() instead
24
-- private property: _lastFrame
25
-- what keys were pressed last frame
29
new = function (self, obj)
30
obj = self:extend(obj)
32
love.keypressed = function (key, unicode) obj:keyPressed(key, unicode) end
33
love.keyreleased = function (key, unicode) obj:keyReleased(key, unicode) end
34
if obj.onNew then obj:onNew() end
39
-- Are *any* of the keys passed held down this frame?
42
-- string key descriptions passed as individual arguments
47
pressed = function (self, ...)
49
for _, value in pairs(keys) do
51
assert(type(value) == 'string', 'all keys are strings; asked to check a ' .. type(value))
54
if self._thisFrame[value] then
62
-- Method: justPressed
63
-- Are *any* of the keys passed pressed for the first time this frame?
66
-- string key descriptions passed as individual arguments
71
justPressed = function (self, ...)
74
for _, value in pairs(keys) do
76
assert(type(value) == 'string', 'all keys are strings; asked to check a ' .. type(value))
79
if self._thisFrame[value] and not self._lastFrame[value] then
88
-- Are *all* of the keys passed not held down this frame?
91
-- string key descriptions passed as individual arguments
96
released = function (self, ...)
99
for _, value in pairs(keys) do
101
assert(type(value) == 'string', 'all keys are strings; asked to check a ' .. type(value))
104
if self._thisFrame[value] then
112
-- Method: justReleased
113
-- Are *any* of the keys passed released after being held last frame?
116
-- string key descriptions passed as individual arguments
121
justReleased = function (self, ...)
124
for _, value in pairs(keys) do
126
assert(type(value) == 'string', 'all keys are strings; asked to check a ' .. type(value))
129
if self._lastFrame[value] and not self._thisFrame[value] then
137
-- Method: allPressed
138
-- Returns all buttons currently pressed this frame.
144
-- string key descriptions; if nothing is pressed, nil
146
allPressed = function (self)
149
for key, value in pairs(self._thisFrame) do
150
if value then table.insert(result, key) end
153
return unpack(result)
156
-- Method: allJustPressed
157
-- Returns all keys just pressed this frame.
163
-- string key descriptions; if nothing is just pressed, nil
165
allJustPressed = function (self)
168
for key, value in pairs(self._thisFrame) do
169
if value and not self._lastFrame[key] then table.insert(result, key) end
172
return unpack(result)
175
-- Method: allJustReleased
176
-- Returns all keys just released this frame.
182
-- string key descriptions; if nothing is just pressed, nil
184
allJustReleased = function (self)
187
for key, value in pairs(self._thisFrame) do
188
if not value and self._lastFrame[key] then table.insert(result, key) end
191
return unpack(result)
194
-- Converts a character code to a Unicode string
195
-- see http://stackoverflow.com/questions/7780179/what-is-the-way-to-represent-a-unichar-in-lua/7799843
197
unicodeChar = function (self, code)
198
if code == nil then return nil end
199
if code < 32 then return string.format('\\x%02x', code) end
200
if code < 126 then return string.char(code) end
201
if code < 65539 then return string.format("\\u%04x", code) end
202
if code < 1114111 then return string.format("\\u%08x", code) end
205
-- Connects to the love.keypressed callback
207
keyPressed = function (self, key, unicode)
208
self._thisFrame[key] = true
209
if unicode and unicode >= 0x20 and unicode ~= 127 and unicode < 0x3000 then
210
self.typed = self.typed .. self:unicodeChar(unicode)
213
-- aliases for modifiers
215
if key == 'rshift' or key == 'lshift' or
216
key == 'rctrl' or key == 'lctrl' or
217
key == 'ralt' or key == 'lalt' or
218
key == 'rmeta' or key == 'lmeta' or
219
key == 'rsuper' or key == 'lsuper' then
220
self._thisFrame[string.sub(key, 2)] = true
224
-- Connects to the love.keyreleased callback
226
keyReleased = function (self, key, unicode)
227
self._thisFrame[key] = false
229
-- aliases for modifiers
231
if key == 'rshift' or key == 'lshift' or
232
key == 'rctrl' or key == 'lctrl' or
233
key == 'ralt' or key == 'lalt' or
234
key == 'rmeta' or key == 'lmeta' or
235
key == 'rsuper' or key == 'lsuper' then
236
self._thisFrame[string.sub(key, 2)] = false
240
endFrame = function (self, elapsed)
241
for key, value in pairs(self._thisFrame) do
242
self._lastFrame[key] = value
246
Sprite.endFrame(self, elapsed)
249
update = function() end