469,280 Members | 1,818 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,280 developers. It's quick & easy.

Python - get key pressing ?

Hi !

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 !

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

What I need to catch the keyboard events - without stopping a script ?

Example:

while False:
DoWorkPeriodic()
if KeyEvent=='B':
SaveWork
Stopit

KK

Jul 18 '05 #1
3 12980
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).

-Peter
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Paul Miller | last post: by
699 posts views Thread by mike420 | last post: by
46 posts views Thread by Scott Chapman | last post: by
1 post views Thread by sarmin kho | last post: by
1 post views Thread by bill ramsay | last post: by
13 posts views Thread by Ramza Brown | last post: by
18 posts views Thread by qwweeeit | last post: by
25 posts views Thread by abhinav | last post: by
19 posts views Thread by diffuser78 | last post: by
3 posts views Thread by cosmo_general | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.