Skip to content

Instantly share code, notes, and snippets.

@MAGANER
Created May 27, 2023 15:10
Show Gist options
  • Save MAGANER/30234e234cec0a527eedf1e28cf7c8ae to your computer and use it in GitHub Desktop.
Save MAGANER/30234e234cec0a527eedf1e28cf7c8ae to your computer and use it in GitHub Desktop.
secret input to hide characters, entered by user.
import msvcrt
import sys
def secret_input(prompt,char=""):
'''get terminal input without showing the input'''
fb = lambda n: int.from_bytes(n,byteorder="big")# lambda to convert bytes into int
#show prompt, then flush buffer to redraw
print(prompt,end="")
sys.stdout.flush()
#get input untill ENTER key is pressed
result_value = ""
while (pressed := msvcrt.getch()) and fb(pressed) != 13:
if char != "":# if there is specific character, show it instead of input
print(char,end="")
sys.stdout.flush()
result_value = result_value +chr(fb(pressed))
return result_value
#example of use
#print(secret_input("enter password:",char="*"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment