Keypress Input

Paul Rubin no.email at nospam.invalid
Tue Jun 16 17:22:38 EDT 2015


John McKenzie <davros at bellaliant.net> writes:
>  Would like a set-up where something happens when a key is pressed. Not 
> propose a question, have the user type something, then hit return, then 
> something happens, but just the R key is pressed, something happens, 

The quick answer is that you want raw mode tty input.  Type "stty raw"
to the shell, and sys.stdin.read(1) will give you the bytes of the typed
characters immediately.  Use "stty -raw" to put it back in cooked mode,
or maybe better, "stty sane" to restore normal settings in general.

Yes there are messy details like function keys actually sending several
characters.  In practice you'll also want to control the tty modes from
your program instead of the shell, but that should get you started.  You
may also want to turn off echo, i.e. "stty raw -echo".



More information about the Python-list mailing list