--[[ Wireless Keyboard - Sender Usage: wkey 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 - Sender ===") print("Channel: " .. channel) print("Status: Connected") print("-----------------------------------") print("Input: ") print("-----------------------------------") print("[Ctrl+T to quit]") local input = "" local cursorPos = 1 local inputLineY = 5 -- line where "Input: " is printed local function updateInput() term.setCursorPos(1, inputLineY + 1) term.clearLine() term.write("> " .. input) -- clear extra spaces local spaces = 40 - #input if spaces > 0 then term.write(string.rep(" ", spaces)) end -- set cursor position local cursorX = 3 + cursorPos -- "> " is 2 chars, so cursor start at 3? -- Actually "> " length is 2, so cursorX = 2 + cursorPos + 1? Let's calculate: -- We write "> " then input, so the cursor should be after "> " + (cursorPos-1) chars. cursorX = 2 + cursorPos term.setCursorPos(cursorX, inputLineY + 1) term.setCursorBlink(true) end updateInput() local function sendData(data) modem.transmit(channel, channel, data) end while true do local event, p1, p2 = os.pullEvent() if event == "char" then local char = p1 input = input:sub(1, cursorPos - 1) .. char .. input:sub(cursorPos) cursorPos = cursorPos + 1 sendData({ type = "char", data = char }) updateInput() elseif event == "key" then local keyCode = p1 local isHeld = p2 if keyCode == keys.backspace then if cursorPos > 1 then input = input:sub(1, cursorPos - 2) .. input:sub(cursorPos) cursorPos = cursorPos - 1 sendData({ type = "control", data = "backspace" }) updateInput() end elseif keyCode == keys.enter then if #input > 0 then sendData({ type = "line", data = input }) end sendData({ type = "control", data = "enter" }) input = "" cursorPos = 1 updateInput() elseif keyCode == keys.left then if cursorPos > 1 then cursorPos = cursorPos - 1 updateInput() end elseif keyCode == keys.right then if cursorPos <= #input then cursorPos = cursorPos + 1 updateInput() end elseif keyCode == keys.home then cursorPos = 1 updateInput() elseif keyCode == keys["end"] then cursorPos = #input + 1 updateInput() elseif keyCode == keys.delete then if cursorPos <= #input then input = input:sub(1, cursorPos - 1) .. input:sub(cursorPos + 1) sendData({ type = "control", data = "delete" }) updateInput() end end elseif event == "terminate" then print("\nWireless keyboard stopped.") break end end