3

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 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Will this prevent the user being able to see which key they pressed in the console? I understand the rest of it and think its a great shout just need it not to be seen by the console
@SamHedgecock Yes. This will prevent the user from seeing what they pressed in the console. Try it for yourself. :)
For some reason I cant seem to get this to work within spyder, it seems to get stuck in the loop and pressing enter doesn't break it - this is after fixing an initial error appearing within the get keys module itself to do with concatenating bytes
@SamHedgecock Use another IDE then Spyder. Spyder is a tool for Data Science in Python. Not general programming. This code works, test it in another editor. Use the right tool for the job.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.