[Tutor] How to use getch()?
Dick Moores
rdm at rcblue.com
Mon Aug 28 12:18:55 CEST 2006
At 01:57 AM 8/28/2006, Alan Gauld wrote:
> > while answer == "":
> > c = 0
> > timeStart = time.time()
> > while True:
> > c += 1
> > if msvcrt.kbhit():
> > if msvcrt.getch() == ' ':
> > break
> > if msvcrt.kbhit():
> > if msvcrt.getch() == 'h':
> > print "Hello"
> > if msvcrt.kbhit():
> > if msvcrt.getch() == 'q':
> > answer = "quit"
>
>You only need one kbhit to tell you if any key has been hit at all.
>Then you can use getch to fetch that key and store it in a variable.
>Then the code above turns into:
>
>while True:
> if kbhit(): key = getch()
> if key == ' ': break
> elif key == 'h': print 'hello'
> elif key == 'q': answer = 'quit'
>
>Does that make sense?
Yes! Now my script runs perfectly. And I can add other keys for doing
other things on the fly, as it were. That's what I was after.
Thanks, Alan.
Dick
More information about the Tutor
mailing list