2
-- This tracks the state of the keyboard, i.e. if a key
3
-- was just pressed or released this frame. You can look
4
-- up a key either by its name or its Unicode number.
5
-- Not all keys sensed have Unicode equivalents (e.g. modifiers
6
-- like Control or Alt).
8
-- Only one Keys object can be active at one time. The one currently
9
-- listening to the keyboard can be accessed globally via <the>.keys.
11
-- See http://love2d.org/wiki/KeyConstant for a list of key names.
12
-- This class aliases modifiers for you, so that if you want to check
13
-- whether either the left or right Control key is pressed, you can check
14
-- on 'ctrl' instead of both 'lctrl' and 'rctrl'.
23
-- This is literally what is being typed during the current frame.
24
-- e.g. if the user holds the shift key and presses the 'a' key,
25
-- this will be set to 'A'. Consult <allPressed()> if you
26
-- want to know what specific keys are being pressed.
30
-- private property: _thisFrame
31
-- what keys are pressed this frame
32
-- if you are interested in this, use allPressed() instead
36
-- private property: _lastFrame
37
-- what keys were pressed last frame
41
new = function (self, obj)
42
obj = self:extend(obj)
44
love.keypressed = function (key, unicode) obj:keyPressed(key, unicode) end
45
love.keyreleased = function (key, unicode) obj:keyReleased(key, unicode) end
46
if obj.onNew then obj:onNew() end
51
-- Are *any* of the keys passed held down this frame?
54
-- string key descriptions passed as individual arguments
59
pressed = function (self, ...)
61
for _, value in pairs(keys) do
63
assert(type(value) == 'string', 'all keys are strings; asked to check a ' .. type(value))
66
if self._thisFrame[value] then
74
-- Method: justPressed
75
-- Are *any* of the keys passed pressed for the first time this frame?
78
-- string key descriptions passed as individual arguments
83
justPressed = function (self, ...)
86
for _, value in pairs(keys) do
88
assert(type(value) == 'string', 'all keys are strings; asked to check a ' .. type(value))
91
if self._thisFrame[value] and not self._lastFrame[value] then
100
-- Are *all* of the keys passed not held down this frame?
103
-- string key descriptions passed as individual arguments
108
released = function (self, ...)
111
for _, value in pairs(keys) do
113
assert(type(value) == 'string', 'all keys are strings; asked to check a ' .. type(value))
116
if self._thisFrame[value] then
124
-- Method: justReleased
125
-- Are *any* of the keys passed released after being held last frame?
128
-- string key descriptions passed as individual arguments
133
justReleased = function (self, ...)
136
for _, value in pairs(keys) do
138
assert(type(value) == 'string', 'all keys are strings; asked to check a ' .. type(value))
141
if self._lastFrame[value] and not self._thisFrame[value] then
149
-- Method: allPressed
150
-- Returns all buttons currently pressed this frame.
156
-- string key descriptions; if nothing is pressed, nil
158
allPressed = function (self)
161
for key, value in pairs(self._thisFrame) do
162
if value then table.insert(result, key) end
165
return unpack(result)
168
-- Method: allJustPressed
169
-- Returns all keys just pressed this frame.
175
-- string key descriptions; if nothing is just pressed, nil
177
allJustPressed = function (self)
180
for key, value in pairs(self._thisFrame) do
181
if value and not self._lastFrame[key] then table.insert(result, key) end
184
return unpack(result)
187
-- Method: allJustReleased
188
-- Returns all keys just released this frame.
194
-- string key descriptions; if nothing is just pressed, nil
196
allJustReleased = function (self)
199
for key, value in pairs(self._thisFrame) do
200
if not value and self._lastFrame[key] then table.insert(result, key) end
203
return unpack(result)
206
-- Connects to the love.keypressed callback
208
keyPressed = function (self, key, unicode)
209
self._thisFrame[key] = true
210
if unicode and unicode >= 0x20 and unicode ~= 127 and unicode < 0x3000 then
211
self.typed = self.typed .. string.char(unicode)
214
-- aliases for modifiers
216
if key == 'rshift' or key == 'lshift' or
217
key == 'rctrl' or key == 'lctrl' or
218
key == 'ralt' or key == 'lalt' or
219
key == 'rmeta' or key == 'lmeta' or
220
key == 'rsuper' or key == 'lsuper' then
221
self._thisFrame[string.sub(key, 2)] = true
225
-- Connects to the love.keyreleased callback
227
keyReleased = function (self, key, unicode)
228
self._thisFrame[key] = false
230
-- aliases for modifiers
232
if key == 'rshift' or key == 'lshift' or
233
key == 'rctrl' or key == 'lctrl' or
234
key == 'ralt' or key == 'lalt' or
235
key == 'rmeta' or key == 'lmeta' or
236
key == 'rsuper' or key == 'lsuper' then
237
self._thisFrame[string.sub(key, 2)] = false
241
endFrame = function (self, elapsed)
242
for key, value in pairs(self._thisFrame) do
243
self._lastFrame[key] = value
247
Sprite.endFrame(self, elapsed)
250
update = function() end