Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Password prompt with read() and ASCII bell character (ASCII 7) to hide the password
- local correctPassword = "password1234"
- local enteredPassword = ""
- term.clear()
- term.setCursorPos(1, 1)
- print("Enter password:")
- -- Set the terminal to not display typed characters
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- -- Override the read() method to mask input using ASCII character 7
- local function readPassword()
- local password = ""
- while true do
- local event, key = os.pullEvent("key")
- if key == 13 then -- Enter key
- break
- elseif key == 8 then -- Backspace key
- if #password > 0 then
- password = password:sub(1, -2)
- term.setCursorPos(term.getCursorPos() - 1, term.getCursorPos())
- term.write(" ") -- Erase the last character typed
- term.setCursorPos(term.getCursorPos() - 1, term.getCursorPos())
- end
- else
- local char = string.char(key)
- password = password .. char
- term.setCursorPos(term.getCursorPos() - 1, term.getCursorPos())
- term.write("\7") -- Print ASCII character 7 (bell) to hide the input
- end
- end
- return password
- end
- -- Call the custom readPassword function
- enteredPassword = readPassword()
- -- Check if the entered password matches the correct password
- if enteredPassword == correctPassword then
- print("\nAccess granted!")
- else
- print("\nIncorrect password!")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement