SHOW:
|
|
- or go back to the newest paste.
1 | -- Keyboard API, created by Detective_Smith. Forum topic can be found below. | |
2 | -- http://www.computercraft.info/forums2/index.php?/topic/24268-keyboard/page__pid__231026#entry231026 | |
3 | ||
4 | local keyRepeat = true | |
5 | local keysHeld = {} | |
6 | - | local kgn = keys.getName |
6 | + | |
7 | ||
8 | local function search(key) | |
9 | for i = 1, #keysHeld do | |
10 | - | local keyString = "" |
10 | + | if keysHeld[i] == key then return true end |
11 | end | |
12 | - | keyString = keyString.. (i ~= 1 and i ~= keysHeld and "+" or "") ..keysHeld[i] |
12 | + | |
13 | - | if keyString == key or keysHeld[i] == key then return true end |
13 | + | |
14 | local function apply(key, held) | |
15 | if held then if not search(key) then keysHeld[#keysHeld + 1] = key end | |
16 | else for i = 1, #keysHeld do if keysHeld[i] == key then tblr(keysHeld, i); break end end end | |
17 | end | |
18 | ||
19 | Keyboard = { | |
20 | version = function() return "1.032-NS" end, -- NS stands for Non-String | |
21 | isDown = function(key) return search(key) end, | |
22 | getHeldKeys = function() return keysHeld end, | |
23 | - | version = function() return "1.032" end, |
23 | + | |
24 | getKeyRepeat = function() return keyRepeat end, | |
25 | reset = function() keysHeld = {} end, | |
26 | updateKeys = function(event, key, held) | |
27 | if event == "key" then apply(key, not held or keyRepeat) | |
28 | elseif event == "key_up" then apply(key) | |
29 | - | updateKeys = function(eData) |
29 | + | elseif event == "paste" then apply(keys.v, true) end |
30 | - | if eventData[1] == "key" then apply(kgn(eventData[2]), not eventData[3] or keyRepeat) |
30 | + | |
31 | - | elseif eventData[1] == "key_up" then apply(kgn(eventData[2])) |
31 | + | |
32 | - | elseif eventData[1] == "paste" then apply("v", true) end |
32 | + | |
33 | --[[ | |
34 | local update = Keyboard.updateKeys | |
35 | os.pullEvent = function(...) | |
36 | local eventData = {os.pullEventRaw()} | |
37 | update(eventData[1], eventData[2], eventData[3]) | |
38 | return unpack(eventData) | |
39 | - | local eventData = {os.pullEventRaw()} |
39 | + | |
40 | - | update(eventData); return unpack(eventData) |
40 | + |