[Tutor] Re: getting input from keyboard

Jeff Shannon jeff@ccvcorp.com
Tue Dec 24 14:38:26 2002


Derrick 'dman' Hudson wrote:

>The raw_input() function obtains input from the program's stdin (which
>will be the keyboard unless you use IO redirection).
>

Which (as Dman knows, but the original poster may not) reads a 'line' at 
a time -- it won't return until you hit enter, or (if reading from a 
file/socket/whatever) until a newline character is reached.

>If you are running python on windows, you can import the 'mswin' (or
>somesuch) and obtain a function named getch().
>
>If you want to react to just a single keystroke (rather than
>line-buffered input) and be mostly cross-platform, use the ncurses
>library.
>  
>

I'm pretty sure that the Windows module in question is 'msvcrt', which 
presumably wraps (much of) the mscvrt DLL.  (That's Microsoft Visual C 
Run Time library.)

I'm not sure if ncurses will work at all under Windows, so there really 
isn't any completely cross-platform way to grab a single keystroke. 
 Which, when you consider the number of different platforms, with 
different user-input interfaces, is hardly surprising.  (How do you grab 
keyboard input on a Palm?)

Jeff Shannon
Technician/Programmer
Credit International