How can I mask or hide user input in python without using getpass()?

Viewed 556

as the question suggest I am looking for a way to hide or mask user input within python without using the standard getpass library due to using spyder; I am wondering if this would potentially be possible using key pressed and recording each key pressed without it displaying until say enter is pressed? If this is possible would someone be able to help me with a snippet for this purpose

Thanks, Sam Hedgecock

1 Answers

You should take a look at a kind of key-logging mechanism. You could detect which key the user pressed, store those keys in a buffer to finally output them after ENTER is pressed.

Working cross-platform example:

from getkey import getkey, keys

#Buffer holding all the pressed keys
pressed_keys = []

while True:
    #Getting the pressed key
    key = getkey()

    #Appending it to array
    pressed_keys.append(key)

    #If ENTER is pressed, exit loop
    if key == keys.ENTER:
        break

#Outputting all pressed keys after ENTER is pressed
for key in pressed_keys:
    print(key, end='')

You need to install getkey though. Use pip3 install getkey.

Related