Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Keyboard API, created by Detective_Smith. Forum topic can be found below.
- -- http://www.computercraft.info/forums2/index.php?/topic/24268-keyboard/page__pid__231026#entry231026
- local keyRepeat = true
- local keysHeld = {}
- local kgn = keys.getName
- local tblr = table.remove
- local function search(key)
- local keyString = ""
- for i = 1, #keysHeld do
- keyString = keyString.. (i ~= 1 and i ~= keysHeld and "+" or "") ..keysHeld[i]
- if keyString == key or keysHeld[i] == key then return true end
- end
- end
- local function apply(key, held)
- if held then if not search(key) then keysHeld[#keysHeld + 1] = key end
- else for i = 1, #keysHeld do if keysHeld[i] == key then tblr(keysHeld, i); break end end end
- end
- Keyboard = {
- version = function() return "1.032" end,
- isDown = function(key) return search(key) end,
- getHeldKeys = function() return keysHeld end,
- setKeyRepeat = function(kBool) keyRepeat = kBool end,
- getKeyRepeat = function() return keyRepeat end,
- reset = function() keysHeld = {} end,
- updateKeys = function(eData)
- if eventData[1] == "key" then apply(kgn(eventData[2]), not eventData[3] or keyRepeat)
- elseif eventData[1] == "key_up" then apply(kgn(eventData[2]))
- elseif eventData[1] == "paste" then apply("v", true) end
- end
- }
- --[[
- local update = Keyboard.updateKeys
- os.pullEvent = function(...)
- local eventData = {os.pullEventRaw()}
- update(eventData); return unpack(eventData)
- end
- --]]
Advertisement
Add Comment
Please, Sign In to add comment