input

Jason Orendorff jason at jorendorff.com
Tue Dec 11 19:00:16 EST 2001


"Preben" <preben_er_t0ff at hotmail.com> wrote:
> How do I make a unbufferedinput from the user?
> (So they don't have to hit enter to send)

import msvcrt  ## Windows only
msvcrt.kbhit() tells if input is ready,
msvcrt.getch() gets it without displaying the character,
msvcrt.getche() gets it and also displays the character (e for echo).

or

import termios, tty  ## Unix only
Use termios.tcgetattr() first, to get the normal terminal settings.
Use tty.setcbreak() or tty.setraw() to set the terminal to behave
   in various weird ways.  cbreak is probably what you need.
Use termios.tcsetattr() to restore normal settings when you're done.

# -------------------------------------------------------------------
import termios, TERMIOS, tty
from sys import stdin, stdout

STDIN_FILENO = 0
old_settings = termios.tcgetattr(STDIN_FILENO)
tty.setcbreak(STDIN_FILENO)

try:
    while 1:
        x = stdin.read(1)
        if x == 'q':
            break
        print "you typed chr #%i" % ord(x)
finally:
    # Restore the previous settings
    termios.tcsetattr(STDIN_FILENO, TERMIOS.TCSAFLUSH, old_settings)
# -------------------------------------------------------------------


> Is it possible to make it so that you don't see what you're
> typing.. e.g. to logins?
> or maybe replacing the output with * ??

import getpass
pw = getpass.getpass("enter password: ")

-- 
Jason Orendorff    http://www.jorendorff.com/





More information about the Python-list mailing list