[Tutor] Python 3.0
Mark Tolonen
metolone+gmane at gmail.com
Sun Feb 21 03:28:17 CET 2010
"bob gailer" <bgailer at gmail.com> wrote in message
news:4B7FEA0E.6000805 at gmail.com...
> On 2/20/2010 5:52 AM, Paul Whittaker wrote:
>>
>> Using the following script, the (Windows 7) PC echoes the key presses
>> but doesn't quit (ever!)
>>
>> import msvcrt
>>
>> print('Press SPACE to stop...')
>>
>> while True:
>>
>> k = msvcrt.getch() # Strangely, this echoes to the IDLE window
>>
>> if k == ' ': # Not recognized
>>
>> break
>>
>
>
> According to the docs: "msvcrt.getch() - Read a keypress and return the
> resulting character....This call will block if a keypress is not already
> available, but will not wait for Enter to be pressed. If the pressed key
> was a special function key, this will return '\000' or '\xe0'; the next
> call will return the keycode. The Control-C keypress cannot be read with
> this function."
>
> IOW - getch always returns 1 character.
>
> You are testing for a 0 length string. Will never happen.
>
> What keystroke do you want to break the loop?
To Bob: There is a space between the quotes.
To the OP: Are you running from a console window? The code works fine for
me from the console, but running under both IDLE and PythonWin
msvcrt.getch() returns 255 immediately. Windows applications without a
console usually don't play nice with C I/O calls.
-Mark
More information about the Tutor
mailing list