DEV Community

Cover image for Secret input in python, Get input secretly and in a personalized way!
Matin
Matin

Posted on • Edited on

Secret input in python, Get input secretly and in a personalized way!

In the Python programming language, we use the input () command to get input from the user, which we all know. But sometimes we want to get the input so that the user actually enters it, but it can not be displayed in command prompt! In these cases, we use the local ‘’getpass‘’ library.
Now we want to write a secret input function together that performs the getpass operation, but makes the appearance of the program more beautiful and hides the entries with a star or the custom display. (Like a password entry in html)

Alt Text

Since we are not allowed to perform an operation when receiving a simple input in Python, let’s use pynput and get the input of the user keyboard
In this short article, I want to make the code of this class available to you, but before that, I will give you a brief description.

How it works?

This class receives your input with pynput and performs the simulation completely with a few simple terms and prints.
The function of this function is such that the user input is received using pynput, but because pynput receives the user input in any place and we only want to receive the input that the user enters in the console, a function to receive the active window in the form of cross We write -platform and using this function, we put the condition in on_press that the inputs are received only in the console.
To display the stars and any other costume characters as input, we must print it with a blank end. But it is not possible to print them using print ()! So we do this using the sys module and the sys.stdout command and print the expressions. Click to read a little about sys.stdout printing.
Until now, inputs are received and a star or any other character is printed while typing! Now we want to make sure that if the character was wrong and we hit backspace, that character will be deleted from the console.
If we pay attention, the inputs that are sent in on_press have a value called char that we can receive the value. But for keys such as Inter, backspace, arrow keys, etc., it is not like this and it is raised. So we bet that if the key entered is equal to pynput.keyboard.Key.backspace, the backup operation will be performed and a return will be made so that the continuation of the function code does not work. Now how to clear the input from the console? Just print the phrase ‘\b \b’ using sys.stdout. To be precise, each \b reverses the input once, but the character is not deleted after that. So we put a space after that to replace the input with a space after that and the character is removed from the user’s view and we go back again with \b once as if there was a backspace in the input once.
Note that if you want to get the show to the user as desired, the user may give a multi-digit show. So multiply the phrase ‘\b \b’ by the number of show characters so that there is no problem.
Done! So far we can type and delete the typed phrase. Be a little careful. If we hit too much backspace, the custom printed prompt will also be deleted. To prevent this, we create a numeric variable called on_char and set its value to 0. For each on_press we add a number to this variable. But we bet on backspace that if this variable is less than or equal to 0, it will retry and the function will not run. This way we can prevent this problem.
So far we have made a show entry! Now we want to receive the user inputs and save them in a variable so that when the user enters the enter key, those saved inputs will be returned. Simply create a self.value in our init class and put the key.char in each on_press, which is equivalent to the user input string character, and in the case of backspace, by converting the self.value to a list, and popping the last character And convert self.value to string again using join, delete the last letters.
Note that for this section, put a try and except because it is possible to enter separation keys that do not have a value of char, so if except, do a simple return.
For when the user enters the enter key, because we turned on the listener by join method and we can not simply stop the listener, so we start the listener in a fan and on_press in the same function. We put that function privately in init so that the user can not use it.
The problem with pynput is that when we press the Enter key, once the phrases we have typed so far are pasted and entered! To prevent this, if the user enters enter, we put an empty getpass.getpass to take this input imperceptibly!
Almost done! Now call the function exactly below init and then return the self.value value in str. For added security, you can delete variables other than self.value with del. The codes of this useful class are available here:

Code

Top comments (2)

Collapse
 
shudo profile image
Shudo

Hello, after several searches I came across your post but I'm a bit annoyed because I can't get the numeric pad keys to listen when I press one of them it displays a code for example ( <96> for the number 0 ) and I can't solve this problem in the documentation it is specified to add this ( num_lock = ) but trying several techniques with this method I can't seem to get the numeric pad to listen would you have the solution please?

Collapse
 
matin profile image
Matin

Hello. Since pynput does not have a converter for the num pad, you have to convert the key number to 0-9 yourself, which is solved by subtracting 96 from the keyboard code.
Thanks for your comment and report :) , the SecretInput class code was updated and the issue was solved.