0

I want getstr to display * (Asterisk) when a key is pressed, basically its like a password login form. Any clues on how to do that ? (Samiliar to getpass)

So if i inputed abcd it would return **** (abcd)

I don't know if this question already been answered.

RaphielHS
  • 31
  • 7

1 Answers1

0

You can take the input from the user as series of characters via getch()

then print * instead after each input

[EDIT]:

Forgot to handle the backspace key: -

Please refer to the below more robust code:

import sys
import msvcrt

def encriptAndsavePassword():
    password = ''
    while True:
        x = ord(msvcrt.getch())
        if x == 13:
            # When system receives a carriage return character
            sys.stdout.write('\n')
            return password
        elif x == 8:
            if len(password) > 0:
                # Deletes previous character.
                sys.stdout.write('\b' + ' ' + '\b')                
                sys.stdout.flush()
                password = password[:-1] 
        else:
            sys.stdout.write('*')
            password +=x

you can read about getch() from here

Pratap Alok Raj
  • 1,098
  • 10
  • 19