Dick Moores wrote: > At 09:51 AM 8/27/2006, Kent Johnson wrote: > >> Dick Moores wrote: >> >>> I'm trying to figure out how to change what a script does while it is >>> running, by pressing a key, such as "k". Can getch() be used for >>> this? >>> >> Google 'python getch' or see this recipe by our very own Danny Yoo: >> > > So now I have, thanks to Danny Yoo: > > ===================================== > class _Getch: > """Gets a single character from standard input. Does not echo to the > screen.""" > def __init__(self): > try: > self.impl = _GetchWindows() > except ImportError: > self.impl = _GetchUnix() > > def __call__(self): return self.impl() > > class _GetchWindows: > def __init__(self): > import msvcrt > > def __call__(self): > import msvcrt > return msvcrt.getch() > > c = 0 > getch = _Getch() > while True: > c += 1 > if getch == "k": > This should be if getch() == "k":
getch is a callable object (it implements __call__()) and is used like a function. If you only want to run on Windows, as your code suggests, just call msvcrt.getch() directly. This is a blocking call - it won't return until a key is pressed. If you don't want to block, use msvcrt.kbhit() to check whether a key is available before calling getch(). > break > print c > ================================================ > > No more errors, but it doesn't do what I wanted. More help, please. > > Dick Moores > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > > _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor