SHARE
TWEET

modem.lua

a guest Mar 24th, 2019 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local modem = require("component").modem
  2. local event = require("event")
  3. local keyboard = require("keyboard")
  4. local term = require("term")
  5.  
  6. local run = true
  7. local message = ""
  8.  
  9. modem.open(123)
  10.  
  11. if modem.isOpen(123) then
  12.   print("Open!")
  13. else
  14.   print("Sorry")
  15. end
  16.  
  17. function mod (_,_, sender, port, distance, msg)
  18.   print("[Port: " .. port .. " Distance: " .. distance .. "] " .. msg)
  19.   if msg == "stop" then
  20.     run = false
  21.   end
  22. end
  23.  
  24. function k_d (_,_, char, code)
  25.   term.clear()
  26.   local key = keyboard.keys[code]
  27.   if key == "enter" then
  28.     modem.broadcast(123, message)
  29.     message = ""
  30.   elseif key == "space" then
  31.     message = message .. " "
  32.   elseif key == "back" then
  33.     message = ""
  34.   elseif key == "f1" then
  35.     run = false
  36.   else
  37.     message = message .. key
  38.     print(message)
  39.   end
  40. end
  41.  
  42. event.listen("key_down", k_d)
  43. event.listen("modem_message", mod)
  44.  
  45. while run do
  46.   os.sleep(0.1)
  47. end
  48.  
  49. event.ignore("key_down", k_d)
  50. event.ignore("modem_message", mod)
  51.  
  52. os.exit()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top