How can I handle the char immediately after its input, without waiting an endline?

Terry Reedy tjreedy at udel.edu
Wed Oct 22 12:39:15 EDT 2008


rishi pathak wrote:
> The below piece of code should give you some understanding
> 
> import tty
> import sys
> tty.setraw(sys.stdin.fileno())
> char=''
> print "Press x to exit"
> while char != 'x' :
>         char = sys.stdin.read(1)
>         print "You entered : ",char
>         # Your code here

Does not work on Windows, at least with 3.0, as tty fails trying to 
import termios.  There, use msvcrt module
"msvcrt.kbhit()
Return true if a keypress is waiting to be read.
msvcrt.getch()
Read a keypress and return the resulting character. Nothing is echoed to 
the console. This call will block if a keypress is not already 
available, but will not wait for Enter to be pressed. If the pressed key 
was a special function key, this will return '\000' or '\xe0'; the next 
call will return the keycode. The Control-C keypress cannot be read with 
this function.
"

> 
> 
> On Wed, Oct 22, 2008 at 2:34 PM, Lave <lave.wang.w at gmail.com 
> <mailto:lave.wang.w at gmail.com>> wrote:
> 
>     Hi, all.
> 
>     I'm a new comer. So This question maybe sutpid.:)
> 
>     I want to write something that handle every char immediately after its
>     input. Then tehe user don't need to type [RETURN] each time. How can I
>     do this?
> 
>     Thanks in advance.
> 
> 
>     --
>     Regards
> 
>     Lave
>     --
>     http://mail.python.org/mailman/listinfo/python-list
> 
> 
> 
> 
> -- 
> Regards--
> Rishi Pathak
> Pune-Maharastra
> 
> 
> ------------------------------------------------------------------------
> 
> --
> http://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list