Advertisement
DOGGYWOOF

Untitled

Dec 17th, 2024 (edited)
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. -- Password prompt with read() and ASCII bell character (ASCII 7) to hide the password
  2. local correctPassword = "password1234"
  3. local enteredPassword = ""
  4.  
  5. term.clear()
  6. term.setCursorPos(1, 1)
  7. print("Enter password:")
  8.  
  9. -- Set the terminal to not display typed characters
  10. term.setTextColor(colors.white)
  11. term.setBackgroundColor(colors.black)
  12.  
  13. -- Override the read() method to mask input using ASCII character 7
  14. local function readPassword()
  15. local password = ""
  16. while true do
  17. local event, key = os.pullEvent("key")
  18.  
  19. if key == 13 then -- Enter key
  20. break
  21. elseif key == 8 then -- Backspace key
  22. if #password > 0 then
  23. password = password:sub(1, -2)
  24. term.setCursorPos(term.getCursorPos() - 1, term.getCursorPos())
  25. term.write(" ") -- Erase the last character typed
  26. term.setCursorPos(term.getCursorPos() - 1, term.getCursorPos())
  27. end
  28. else
  29. local char = string.char(key)
  30. password = password .. char
  31. term.setCursorPos(term.getCursorPos() - 1, term.getCursorPos())
  32. term.write("\7") -- Print ASCII character 7 (bell) to hide the input
  33. end
  34. end
  35. return password
  36. end
  37.  
  38. -- Call the custom readPassword function
  39. enteredPassword = readPassword()
  40.  
  41. -- Check if the entered password matches the correct password
  42. if enteredPassword == correctPassword then
  43. print("\nAccess granted!")
  44. else
  45. print("\nIncorrect password!")
  46. end
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement