Processing a key pressed in Python 3.6

Virgil Stokes vs at it.uu.se
Tue Jan 23 20:13:35 EST 2018


Ok Dennis,

You were correct. The following also works in the Windows 10 command window.

> import time
> import msvcrt
>
> while "more work to do":
>     print("Getting data...")
>     time.sleep(1)
>     print("Saving data to file...")
>     time.sleep(1)
>     key = msvcrt.getwch()
>     #print('key: %s'%key)  # just to show the result
>     if key == chr(27):
>         print("Pre-termination...")
>         time.sleep(1)
>         raise SystemExit("exit") 
Note, I am using the "Esc" key to exit, which is one answer to my last 
posting on this topic.


On 2018-01-23 20:37, Dennis Lee Bieber wrote:
> On Tue, 23 Jan 2018 19:50:57 +0100, Virgil Stokes <vs at it.uu.se> declaimed
> the following:
>
>> I am running the code with Python 3.6 on a windows 10 platform. I have
>> tried many approaches (mainly those posted on stackoverflow) but I have
>> yet to find an approach that works for this structure.
>>
>> Note:
>>    1) The code is executed in the windows 10 command window
>>    2) I am not using wxPython, IDLE, or pyGame in this application.
>>    3) The time to get the data, process it and write it to a file can
>>       take from 0.5 sec to 1.5 sec
>>    4) The key hit need not be echoed to the command window
> 	And none of your searching found
> https://docs.python.org/3/library/msvcrt.html
> which is part of the standard library (and documented in the library
> manual). The module IS Windows specific, you'd have to rewrite the code to
> run on Linux.
>
> 	Now, if your requirement was to detect a keypress WHILE the data
> fetch/processing was happening, the solution will be different.




More information about the Python-list mailing list