Processing a key pressed in Python 3.6

Virgil Stokes vs at it.uu.se
Wed Jan 24 15:02:39 EST 2018


Yes, I am aware of this Dennis. However, but, on my system I actually 
had it running without the msvcrt.kbhit()

This occurred during testing while trying different options. And I was 
able to reproduce this several times. Why? I do not have an answer. This 
is one reason why I posted the code. It would be interesting to know if 
anyone else has obtained the same results.

Note: 1) 3.6.2 (v3.6.2:5fd33b5, Jul  8 2017, 04:57:36) [MSC v.1900 64 
bit (AMD64)]
            2) msvcrt is built-in (at least on my system)

On 2018-01-24 18:28, Dennis Lee Bieber wrote:
> On Wed, 24 Jan 2018 02:13:35 +0100, Virgil Stokes <vs at it.uu.se>*declaimed*  ?
> the following:
>
>
>
>>>      key = msvcrt.getwch()
> 	NOTE: per documentation, that is a blocking read... It won't return
> unless a key (any key) has been pressed. That means your "more work to do"
> loop requires a key press for each loop.
>
> 	Recommendation would be to use kbhit() first.
>
>
> 	if msvcrt.kbhit():
> 		key = msvcrt.getwch()
> 		...
>
>
> 	Consider
>
> -=-=-=-=-
>
> import msvcrt as ms
> import time
>
> ESCAPE = chr(27)
>
> workPending = True
> cycle = 0
>
> while workPending:
>      print("Getting imaginary data %s..." % cycle)
>      time.sleep(1.5)
>      print("\tSaving imaginary data %s..." % cycle)
>      time.sleep(1.5)
>
>      if ms.kbhit():
>          key = ms.getwch()
>          if key == ESCAPE:
>              print("Pre-termination on cycle %s..." % cycle)
>              break
>          else:
>              print("Random keypress %r found on cycle %s..." % (key, cycle))
>
>      cycle += 1
>
> time.sleep(1.5)
> print("Termination")
> -=-=-=-=-
> C:\Users\Wulfraed\Documents\Python Progs>kbhit
> Getting imaginary data 0...
>          Saving imaginary data 0...
> Getting imaginary data 1...
>          Saving imaginary data 1...
> Random keypress u'd' found on cycle 1...
> Getting imaginary data 2...
>          Saving imaginary data 2...
> Getting imaginary data 3...
>          Saving imaginary data 3...
> Random keypress u'a' found on cycle 3...
> Getting imaginary data 4...
>          Saving imaginary data 4...
> Pre-termination on cycle 4...
> Termination
>
> C:\Users\Wulfraed\Documents\Python Progs>




More information about the Python-list mailing list