You can create your own version of the input
function by getting keystrokes silently with the msvcrt.getch
function in a loop. With each keystroke, you either append it to a list and output it to the console, or abort the loop if it's a carriage return.
To deal with a backspace, pop the last character from the list and output a backspace, then a space to erase the last character from the console, and then another backspace to actually move the cursor backwards.
Note that msvcrt.getch
works only in Windows, and you should install the getch package instead in other platforms:
try:
from msvcrt import getch
except ModuleNotFoundError:
from getch import getch
def input_no_newline(prompt=''):
chars = []
print(prompt, end='', flush=True)
while True:
char = getch().decode()
if char == '\r':
break
if char != '\b':
chars.append(char)
print(char, end='', flush=True)
elif chars:
chars.pop()
print('\b \b', end='', flush=True)
return ''.join(chars)
user = input_no_newline("Type Here: ")
print("Text")