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'.
24
-- This is literally what is being typed during the current frame.
25
-- e.g. if the user holds the shift key and presses the 'a' key,
26
-- this will be set to 'A'. Consult <allPressed()> if you
27
-- want to know what specific keys are being pressed.
31
-- private property: _thisFrame
32
-- what keys are pressed this frame
33
-- if you are interested in this, use allPressed() instead
37
-- private property: _lastFrame
38
-- what keys were pressed last frame
42
new = function (self, obj)
43
obj = self:extend(obj)
45
love.keypressed = function (key, unicode) obj:keyPressed(key, unicode) end
46
love.keyreleased = function (key, unicode) obj:keyReleased(key, unicode) end
47
if obj.onNew then obj:onNew() end
52
-- Are *any* of the keys passed held down this frame?
55
-- string key descriptions passed as individual arguments
60
pressed = function (self, ...)
62
for _, value in pairs(keys) do
64
assert(type(value) == 'string', 'all keys are strings; asked to check a ' .. type(value))
67
if self._thisFrame[value] then
75
-- Method: justPressed
76
-- Are *any* of the keys passed pressed for the first time this frame?
79
-- string key descriptions passed as individual arguments
84
justPressed = function (self, ...)
87
for _, value in pairs(keys) do
89
assert(type(value) == 'string', 'all keys are strings; asked to check a ' .. type(value))
92
if self._thisFrame[value] and not self._lastFrame[value] then
101
-- Are *all* of the keys passed not held down this frame?
104
-- string key descriptions passed as individual arguments
109
released = function (self, ...)
112
for _, value in pairs(keys) do
114
assert(type(value) == 'string', 'all keys are strings; asked to check a ' .. type(value))
117
if self._thisFrame[value] then
125
-- Method: justReleased
126
-- Are *any* of the keys passed released after being held last frame?
129
-- string key descriptions passed as individual arguments
134
justReleased = function (self, ...)
137
for _, value in pairs(keys) do
139
assert(type(value) == 'string', 'all keys are strings; asked to check a ' .. type(value))
142
if self._lastFrame[value] and not self._thisFrame[value] then
150
-- Method: allPressed
151
-- Returns all buttons currently pressed this frame.
157
-- string key descriptions; if nothing is pressed, nil
159
allPressed = function (self)
162
for key, value in pairs(self._thisFrame) do
163
if value then table.insert(result, key) end
166
return unpack(result)
169
-- Method: allJustPressed
170
-- Returns all keys just pressed this frame.
176
-- string key descriptions; if nothing is just pressed, nil
178
allJustPressed = function (self)
181
for key, value in pairs(self._thisFrame) do
182
if value and not self._lastFrame[key] then table.insert(result, key) end
185
return unpack(result)
188
-- Method: allJustReleased
189
-- Returns all keys just released this frame.
195
-- string key descriptions; if nothing is just pressed, nil
197
allJustReleased = function (self)
200
for key, value in pairs(self._thisFrame) do
201
if not value and self._lastFrame[key] then table.insert(result, key) end
204
return unpack(result)
207
-- Converts a character code to a Unicode string
208
-- see http://stackoverflow.com/questions/7780179/what-is-the-way-to-represent-a-unichar-in-lua/7799843
210
unicodeChar = function (self, code)
211
if code == nil then return nil end
212
if code < 32 then return string.format('\\x%02x', code) end
213
if code < 126 then return string.char(code) end
214
if code < 65539 then return string.format("\\u%04x", code) end
215
if code < 1114111 then return string.format("\\u%08x", code) end
218
-- Connects to the love.keypressed callback
220
keyPressed = function (self, key, unicode)
221
self._thisFrame[key] = true
222
if unicode and unicode >= 0x20 and unicode ~= 127 and unicode < 0x3000 then
223
self.typed = self.typed .. self:unicodeChar(unicode)
226
-- aliases for modifiers
228
if key == 'rshift' or key == 'lshift' or
229
key == 'rctrl' or key == 'lctrl' or
230
key == 'ralt' or key == 'lalt' or
231
key == 'rmeta' or key == 'lmeta' or
232
key == 'rsuper' or key == 'lsuper' then
233
self._thisFrame[string.sub(key, 2)] = true
237
-- Connects to the love.keyreleased callback
239
keyReleased = function (self, key, unicode)
240
self._thisFrame[key] = false
242
-- aliases for modifiers
244
if key == 'rshift' or key == 'lshift' or
245
key == 'rctrl' or key == 'lctrl' or
246
key == 'ralt' or key == 'lalt' or
247
key == 'rmeta' or key == 'lmeta' or
248
key == 'rsuper' or key == 'lsuper' then
249
self._thisFrame[string.sub(key, 2)] = false
253
endFrame = function (self, elapsed)
254
for key, value in pairs(self._thisFrame) do
255
self._lastFrame[key] = value
259
Sprite.endFrame(self, elapsed)
262
update = function() end