[Possible SPAM] Re: Using msvcrt (in Windows), how to catch Enter key?

Dick Moores rdm at rcblue.com
Mon Oct 29 20:22:36 EDT 2007


At 03:23 PM 10/29/2007, Gabriel Genellina wrote:
>En Mon, 29 Oct 2007 14:39:49 -0300, Dick Moores <rdm at rcblue.com> escribió:
>
> > But here's a case where it seems I do need the
> >
> > if msvcrt.kbhit() line
>
>At least add a small sleep() call inside the loop, to be nice to other
>running processes:
>
> > =========================
> > #!/usr/bin/env python
> > #coding=utf-8
> > import time
> > import msvcrt
> > timeNow = time.time()
> > oldTimeNow = timeNow
> > while True:
> >      if msvcrt.kbhit():
> >          key = msvcrt.getch()
> >          if key == 'h':
> >              print 'Hello'
> >          if key == 'b':
> >              print 'Bye'
> >          if key == '\r': # Enter key
> >              break
>         else:
>             time.sleep(0.1)
> >      timeNow = time.time()
> >      if timeNow - oldTimeNow > 5:
> >          print "5 seconds passed"
> >          oldTimeNow = timeNow
> > =========================

Yes, that makes a major difference in the CPU 
usage percentage on my computer. In fact I can't 
even tell that there is anything going on other 
than the usual behind-the-scenes XP stuff. CPU 
usage stays right around 0% or 6%, with an 
occasional 6% and a very occasional 15%. 
Interestingly, sleep(0.001) makes as big a 
difference as your sleep(0.1), but sleep(0.0001) bumps it up to a steady 100%!

Thanks,

Dick 




More information about the Python-list mailing list