A portable way to read a single character from the keyboard??

Matthew Dixon Cowles matt at mondoinfo.com
Sun Sep 10 00:20:26 CEST 2000


On Sun, 10 Sep 2000 00:03:06 +0200, Iwan van der Kleyn
<ivdkleyn at xs4all.nl> wrote:

>I want to implement a simple console-menu. The script needs to read
>single characters from the keyboard. In this way the user only needs
>to press a sinle character in order for a function to be
>performed. Under WIndows you can use the msvcrt module in the
>following way:

[...]

>However, this method is not portable. It cannot be used under Linux
>for example. What would be the portable equivalant? I've tried
>sys.stdin.read(1), but this function insists on blocking the system
>until ENTER is pressed.

Iwan,
Unfortunately, there's no portable way to do what you want despite the
fact that many people would like there to be. Have a look at

http://www.python.org/doc/FAQ.html#4.74

for the best advice that's on offer. You could use sys.platform to
decide what routine to call.

Regards,
Matt



More information about the Python-list mailing list