-1

Am fairly new to python, We have an interesting situation.

We want to read a char from stdin, store the char, and display another char.

example: the user enters 'c', the code is expected to save 'c' and print 'v' (instantaneously) although getpass.getpass saves the input but it print nothing. Am using Python 2.7 running on mac.

HackSparrow
  • 27
  • 2
  • 6

1 Answers1

2

What you're looking for is to "disable echo" in the terminal (aka TTY). Once you have these keywords, searching for solutions becomes a lot easier. Here's one:

How to turn console echo back on after tty.setcbreak()

Basically, to disable "echoing" the user's input to the screen, do this:

import sys
import termios
import tty

old_settings = termios.tcgetattr(sys.stdin)
tty.setcbreak(sys.stdin.fileno())

After that you can use the regular functions to read from stdin and display whatever you want. Then to re-enable terminal echo:

termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)

Note that the above assumes you're working on a Unix-like system.

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436