Python - get key pressing ?

Peter Hansen peter at
Thu Jul 3 16:13:51 CEST 2003

Krisztian Kepes wrote:
> I want to create an py program what process datas for long time.
> But: I want to abort it if I need.
> Not with Ctrl+Break !

Why not?  Ctrl+Break, or more typically Ctrl-C, is the de facto standard 
approach for terminating a console (non-GUI) application.

> If I push a key (B, or other), the script must stop his work, and save the 
> result created before.

In this case, just wrap the code with a "try/except KeyboardInterrupt" and
put your "stop and save" code in the except block.  (Note that Ctrl-Break
will bypass this, but Ctrl-C is caught as you would expect.)

Also, when posting questions of this nature it's a good idea explicitly 
to specify your platform (Linux, Windows, etc).


More information about the Python-list mailing list