hunters2020

Keyboard API

Aug 4th, 2015
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.49 KB | None | 0 0
  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
  7. local tblr = table.remove
  8.  
  9. local function search(key)
  10.     local keyString = ""
  11.     for i = 1, #keysHeld do
  12.         keyString = keyString.. (i ~= 1 and i ~= keysHeld and "+" or "") ..keysHeld[i]
  13.         if keyString == key or keysHeld[i] == key then return true end
  14.     end
  15. end
  16.  
  17. local function apply(key, held)
  18.     if held then if not search(key) then keysHeld[#keysHeld + 1] = key end
  19.     else for i = 1, #keysHeld do if keysHeld[i] == key then tblr(keysHeld, i); break end end end
  20. end
  21.  
  22. Keyboard = {
  23.     version      = function()      return "1.032"     end,
  24.     isDown       = function(key)   return search(key) end,
  25.     getHeldKeys  = function()      return keysHeld    end,
  26.     setKeyRepeat = function(kBool) keyRepeat = kBool  end,
  27.     getKeyRepeat = function()      return keyRepeat   end,
  28.     reset        = function()      keysHeld = {}      end,
  29.     updateKeys   = function(eData)
  30.         if eventData[1] == "key" then apply(kgn(eventData[2]), not eventData[3] or keyRepeat)
  31.         elseif eventData[1] == "key_up" then apply(kgn(eventData[2]))
  32.         elseif eventData[1] == "paste" then apply("v", true) end
  33.     end
  34. }
  35.  
  36. --[[
  37. local update = Keyboard.updateKeys
  38. os.pullEvent = function(...)
  39.         local eventData = {os.pullEventRaw()}
  40.         update(eventData); return unpack(eventData)
  41. end
  42. --]]
Advertisement
Add Comment
Please, Sign In to add comment