[Tutor] How to use getch()?
Alan Gauld
alan.gauld at btinternet.com
Mon Aug 28 10:57:36 CEST 2006
Hi Dick,
I'll move my replies back into the public discussion...
> 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?
Alan G.
More information about the Tutor
mailing list