Using msvcrt (in Windows), how to catch Enter key?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Oct 29 18:23:37 EDT 2007


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
> ==========================

-- 
Gabriel Genellina




More information about the Python-list mailing list