Advertisement
LDDestroier

PB2 Key Input Node

Jan 14th, 2018
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.15 KB | None | 0 0
  1. -- pastebin run 7y3scDEK
  2.  
  3. local program = [[
  4. local modem = peripheral.find("modem")
  5. local side = "right"
  6. modem.open(8)
  7. term.clear()
  8. term.setCursorPos(1,1)
  9. if type(key) == "number" then
  10.     print(keys.getName(key))
  11. else
  12.     print("\""..key.."\"")
  13. end
  14. local mon = peripheral.find("monitor")
  15. local mx,my
  16. if mon then
  17.     mon.setTextScale(2)
  18.     mx,my = mon.getSize()
  19. end
  20. local drawMon = function(invertColors)
  21.     if mon then
  22.         if type(key) == "number" then
  23.             mon.setCursorPos(math.max(1,math.ceil(mx/2)-math.floor(#keys.getName(key)/2)), math.ceil(my/2))
  24.         else
  25.             mon.setCursorPos(math.max(1,math.ceil(mx/2)-math.floor(#key/2)), math.ceil(my/2))
  26.         end
  27.         mon.setTextColor(invertColors and colors.black or colors.white)
  28.         mon.setBackgroundColor(invertColors and colors.white or colors.black)
  29.         mon.clear()
  30.         mon.write((type(key) == "number" and keys.getName(key):upper() or key:upper()))
  31.     end
  32. end
  33. local pushed = false
  34. drawMon(false)
  35. while true do
  36.     local evt = {os.pullEvent()}
  37.     if evt[1] == "monitor_touch" then
  38.         modem.transmit(8,8,{
  39.             ["key"] = key,
  40.             ["status"] = true
  41.         })
  42.         drawMon(true)
  43.         modem.transmit(8,8,{
  44.             ["key"] = key,
  45.             ["status"] = false
  46.         })
  47.         sleep(0.1)
  48.         drawMon(false)
  49.     elseif evt[1] == "redstone" then
  50.         if (rs.getInput(side)) ~= pushed then
  51.             pushed = not pushed
  52.             modem.transmit(8,8,{
  53.                 ["key"] = key,
  54.                 ["status"] = pushed
  55.             })
  56.             if pushed then
  57.                 drawMon(true)
  58.                 if key == "turnon" then
  59.                     local comps = {peripheral.find("computer")}
  60.                     for a = 1, #comps do
  61.                         comps[a].turnOn()
  62.                     end
  63.                 end
  64.             else
  65.                 drawMon(false)
  66.             end
  67.         end
  68.     elseif evt[1] == "modem_message" then
  69.         local message = evt[5]
  70.         if message == "update" then
  71.             if tonumber(key) then
  72.                 shell.run("pastebin run 7y3scDEK "..keys.getName(key))
  73.             else
  74.                 shell.run("pastebin run 7y3scDEK "..key)
  75.             end
  76.         end
  77.     end
  78. end
  79. ]]
  80.  
  81. local tArg = {...}
  82. if not tArg[1] then
  83.     error("specify key")
  84. end
  85.  
  86. local file = fs.open("startup","w")
  87. if not tonumber(keys[tArg[1]]) then
  88.     file.writeLine("key = \""..tArg[1].."\"")
  89. else
  90.     file.writeLine("key = keys[\""..tArg[1].."\"]")
  91. end
  92. file.write(program)
  93. file.close()
  94. os.reboot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement