Advertisement
MatthewJ217

Remote Control v2 (CC Computer)

Jun 1st, 2023 (edited)
879
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.62 KB | None | 0 0
  1. -- Send keyboard inputs as rednet signals to a set ID
  2. peripheral.find("modem", rednet.open);
  3.  
  4. local id = 0;
  5. local event, key;
  6.  
  7. function setID()
  8.     print("Setting new ID\nPress Right Ctrl to set");
  9.     print("Last ID: "..tostring(id));
  10.     key = 0;
  11.     id = 0;
  12.     while true do
  13.         event, key = os.pullEvent("key");
  14.         if key == 345 then
  15.             break
  16.         elseif key > 47 and key < 58 then
  17.             id = id * 10 + (key - 48);
  18.             print(tostring(id));
  19.         end
  20.     end
  21.     print("ID set");
  22. end
  23.  
  24. print("Change ID with Right Ctrl\nMove with wasd\nOther controls are R, U, I, O, P, J, K, 8, and 9.\nPress them at any time to see what they do.");
  25. setID();
  26.  
  27. local directionKeys = {87, 32, 340, 83, 65, 68};
  28.  
  29. local function waitForPressed()
  30.     event, key = os.pullEvent("key");
  31. end
  32.  
  33. local function waitForReleased()
  34.     repeat
  35.     event, key = os.pullEvent("key_up");
  36.  
  37.     local direction = false;   
  38.  
  39.     for index, value in ipairs(directionKeys) do
  40.         if value == key then
  41.             direction = true;
  42.         end
  43.     end
  44.     until direction == false;  
  45.  
  46.     key = 0;
  47. end
  48.  
  49. local function tick()
  50.     while true do
  51.         os.sleep(5)
  52.         print("Tick");
  53.     end
  54. end
  55.  
  56.     -- parallel.waitForAny(waitForPressed, waitForReleased);
  57. while true do
  58.     parallel.waitForAny(waitForPressed, waitForReleased);
  59.     if key == 345 then
  60.         setID();
  61.     else
  62.         rednet.send(id, key);
  63.         local id, message = rednet.receive();
  64.         if key == 49 then
  65.             print("Selected "..message);
  66.         elseif message ~= "" then
  67.             print(message);
  68.         end
  69.     end
  70. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement