Breaking out of a while loop with a key press?
Carl Banks
imbosol-1049135446 at aerojockey.com
Mon Mar 31 14:35:56 EST 2003
Robin Munn wrote:
> Richard <richardd at hmgcc.gov.uk> wrote:
>> Hi,
>>
>> Can anyone suggest a simple way of detecting a key press and existing the
>> program? I have a program which at the moment uses a while True: statement.
>> However this is not ideal as I have to close down the console window to
>> exist the program. Can anyone tell me the proper way of doing this? So that
>> a CTRL-C, for example, can quit the program correctly?
>
> You could use a "try: ... except KeyboardInterrupt: ..." block to detect
> Ctrl-C. Although KeyboardInterrupt *should* by default not be caught, so
> a Ctrl-C *should* be able to shut down your program.
In this case, it shouldn't. He's running his program in a console
window created within Python. The idea of a console is
(theoretically) to give a program complete control of this input;
therefore, console windows often disable KeyboardInterrupt and treat
Control-C like any regular keystroke.
--
CARL BANKS
More information about the Python-list
mailing list