[Tutor] loop until a keypress

Alan Gauld alan.gauld at btinternet.com
Mon Jan 30 09:36:16 CET 2012


On 30/01/12 05:20, Surya K wrote:
> I want to run code until a "enter" is pressed. Well, it shouldn't wait
> for the user to enter "enter"
>
> This is my code:
>
> import msvcrt
>
> chr = 0
> while chr != 'q':
> print "my code",
> if msvcrt.kbhit():
> chr = msvcrt.getch()
>

You shouldn't need the kbhit test.
Try just using:

while ky != 'q':
    ky = msvcrt.getch()
    print ky


> I am on windows. So, as msvcrt is for windows, I wonder if there is any
> module that works for both,

Both what?


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list