getting keyboard input 1 char at a time

Tim Peters tim_one at email.msn.com
Mon Oct 30 05:16:19 EST 2000


[John R Bedal]
> Is there a platform independant method for getting keyboard input one
> character at a time?

[Peter Hansen]
> It's possible John wanted a method which would return "keystrokes", as
> opposed to just whatever was in stdin.
> ...
> On win32, he would have to use msvcrt.getch() or something.
>
> I don't think there is a truly platform-independent method, but I'd be
> happy to be corrected.

Not your lucky day:  Python doesn't offer a portable way to do this because
C doesn't (nothing in the std C language or libraries addresses this).  It
is indeed a x-platform nightmare.  The std getpass.py module is a good one
to study.

msvcrt.getch() and getche() work well in Windows, provided you're running in
a DOS box (not, e.g., in IDLE), and don't try also to read from sys.stdin
(MS doesn't support mixing console I/O with C stdio -- the results are
unpredictable if you try).

the-problem-will-become-moot-once-the-world-moves-to-speech-
    user-interfaces-ly y'rs  - tim






More information about the Python-list mailing list