--[[ Wireless Keyboard - Receiver Usage: wrecv Default channel: 12345 ]] local args = {...} local channel = tonumber(args[1]) or 12345 local modem = peripheral.find("modem") or peripheral.find("wireless_modem") if not modem then print("ERROR: Wireless modem not found. Please attach one.") return end modem.open(channel) term.clear() term.setCursorPos(1, 1) print("=== Wireless Keyboard - Receiver ===") print("Channel: " .. channel) print("Status: Listening") print("-----------------------------------") print("Output:") print("-----------------------------------") print("[Ctrl+T to quit]") print("") print("> ", false) local outputLineY = 8 -- line where "Output:" is, actual output starts at line 9 local cursorX = 3 -- after "> " local cursorY = 8 -- but we write "> " at line 8? Actually after printing "> ", cursor moves. -- Let's just manage cursor position manually. -- We will simply use term.write() and let cursor move naturally, but we need to handle backspace. -- To simplify, we can keep track of current row and column. -- But simpler: just write to screen and let cursor be where it is. -- However, backspace requires special handling. -- We'll use a buffer to store all output text, but that could get large. -- Alternative: just write characters and handle backspace by moving cursor back. -- For this simple version, we'll just write directly and handle backspace by moving cursor. -- We'll keep it simple: when backspace arrives, move cursor back one char and overwrite with space. -- We'll also keep an output string for reference (optional). local outputBuffer = "" -- Function to print received char local function printChar(char) term.write(char) outputBuffer = outputBuffer .. char end -- Function to handle backspace local function handleBackspace() local x, y = term.getCursorPos() if x > 3 and y == outputLineY + 1 then -- we are on the first output line after "> " -- move back, write space, move back term.setCursorPos(x - 1, y) term.write(" ") term.setCursorPos(x - 1, y) outputBuffer = outputBuffer:sub(1, -2) elseif y > outputLineY + 1 then -- go to end of previous line (simplified: assume 50 chars per line) term.setCursorPos(50, y - 1) -- not handling fully, just move to previous line end outputBuffer = outputBuffer:sub(1, -2) end end -- Function to handle enter local function handleEnter() term.write("\n> ", false) outputBuffer = outputBuffer .. "\n" end -- Start printing at the "> " position term.setCursorPos(3, outputLineY + 1) -- assuming outputLineY is the line with "Output:", next line is output -- Actually we already printed "> " at line 8? Let's recalc. -- After printing the header, we printed blank line then "> " at line 8? Let's just set cursor manually after header. -- Simpler: At start, clear, print header, then print "> " and set cursor to after it. term.clear() term.setCursorPos(1,1) print("=== Wireless Keyboard - Receiver ===") print("Channel: " .. channel) print("Status: Listening") print("-----------------------------------") print("Output:") print("-----------------------------------") print("[Ctrl+T to quit]") print("") -- blank line term.write("> ", false) -- write prompt, cursor stays after it -- Now cursor is at column 3, row 9 (if we count rows). We'll store that. local baseRow = 9 local baseCol = 3 -- We'll use term.getCursorPos() to get current position. while true do local event, p1, p2, p3 = os.pullEvent() if event == "modem_message" then local senderId = p1 local replyChannel = p2 local message = p3 if message.type == "char" then local char = message.data term.write(char) elseif message.type == "control" then local control = message.data if control == "backspace" then local x, y = term.getCursorPos() if x > baseCol and y == baseRow then term.setCursorPos(x - 1, y) term.write(" ") term.setCursorPos(x - 1, y) elseif y > baseRow then -- go to end of previous line (assume line width 50) term.setCursorPos(50, y - 1) end elseif control == "enter" then term.write("\n> ", false) -- update baseRow? Actually cursor moves to new line, but we can keep baseRow fixed for backspace handling. -- We'll just set baseRow to current row after writing new prompt. local x, y = term.getCursorPos() baseRow = y baseCol = x elseif control == "delete" then local x, y = term.getCursorPos() if x < 50 and y == baseRow then term.write(" ") term.setCursorPos(x, y) end end elseif message.type == "line" then local line = message.data term.write(line) -- optionally execute command: shell.run(line) end elseif event == "terminate" then print("\n\nReceiver stopped.") break end end