Python - get key pressing ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Krisztian Kepes

    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





  • Jiri Barton

    #2
    Re: Python - get key pressing ?


    Comment

    • Jiri Barton

      #3
      Re: Python - get key pressing ?


      Comment

      • Peter Hansen

        #4
        Re: Python - get key pressing ?

        Krisztian Kepes wrote:[color=blue]
        >
        > 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 ![/color]

        Why not? Ctrl+Break, or more typically Ctrl-C, is the de facto standard
        approach for terminating a console (non-GUI) application.
        [color=blue]
        > If I push a key (B, or other), the script must stop his work, and save the
        > result created before.[/color]

        In this case, just wrap the code with a "try/except KeyboardInterru pt" 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

        Comment

        Working...