Announcement

Collapse
No announcement yet.

using escape key to quit a for/next loop

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • using escape key to quit a for/next loop

    I remember seeing a post about getting out of a for/next loop with the escape key. My searches have not shown any thing. Any ideas would be much appreciated.

    John Tate

  • #2
    Try putting this inside the loop:

    IF GRAHIC INKEY$=CHR$(27) THEN JumpOutOfLoop:

    Comment


    • #3
      Or..
      Code:
      #DIM All
      #COMPILE EXE
      #INCLUDE "WIN32API.INC"
      
      FUNCTION PBMAIN()
       GetAsyncKeyState(%VK_ESCAPE)  ' Clear any previous notification
        Do
          Sleep 100
          If GetAsyncKeyState(%VK_ESCAPE) And &H0001 Then Exit loop
        Loop
      
        MSGBOX Time$,%MB_SystemModal,"Escaped!"
      
      END FUNCTION
      '------------------/PBMain
      Rgds, Dave

      Comment


      • #4
        For/Next specifically...


        FOR LoopCount& = 1 to 200000

        IF GRAPHIC$(INKEY$)=CHR$(27) THEN EXIT FOR

        NEXT LoopCount&

        Comment


        • #5
          Why are we assuming its a graphics application?

          What is wrong with con.inkey$?

          DO UNTIL CON.INKEY$$ = CHR$(27)
          do things
          LOOP
          [I]I made a coding error once - but fortunately I fixed it before anyone noticed[/I]
          Kerry Farmer

          Comment


          • #6
            I am using just a plain for/next loop, not a do/loop. I am not using any graphic commands. I am using PB windows 10.4.

            Comment


            • #7
              EXIT FOR works just as EXIT LOOP does.
              Rgds, Dave

              Comment


              • #8
                I remember seeing a post about getting out of a for/next loop with the escape key.
                AND
                I am using just a plain for/next loop, not a do/loop. I am not using any graphic commands. I am using PB windows 10.4
                If you are looking to exit a loop when <ESC> is pressed, then you HAVE to have something in your FOR/NEXT loop to look for the ESC key, since there is no real Windows' equivalent to the PB/DOS "ON KEY xx GOTO..." statement.

                With multiple threads of execution you have some options...

                GUI + Worker Thread + Abort Demo 11-24-07 (DDT syntax)

                Terminate Worker Threads Using Windows Events (and Using Thread Local Storage) Demo Dec 23 2005

                I think maybe the second one is what you want.

                If this is a GUI program and you are running this FOR/NEXT loop in the main GUI thread, I suggest you could use a bit of redesign.

                Code desiring 'exit loop on <ESC>' not shown.

                MCM
                Michael Mattias
                Tal Systems Inc.
                Racine WI USA
                mmattias@talsystems.com
                http://www.talsystems.com

                Comment


                • #9
                  Thanks Dave Biggs. I see what you mean now. Your suggestion works fine. I use the for/next loop with msgbox "show me something" when testing some data input.

                  John Tate

                  Comment


                  • #10

                    Anne, "Probably it is due to DIALOG END CBHNDL which cannot be used in Win 10", seems that win10 does work OK with DIALOG END CBHNDL .

                    This program is a large program. Three program in one. This program has the same code thru out the program where I have print preview function, and get errors .

                    This is where the program stop working, anywhere I use this print preview code.

                    CALLBACK FUNCTION Addr_Browse_Prt_Cancel() '<----- here seems to be the problem in this cancel callback.
                    ' ''DIALOG ENABLE hDlg3: ????
                    ' ''GRAPHIC DETACH: 'NUM USED IN PRT CURRENT

                    DIALOG ENABLE hDlg2:

                    ' ''CONTROL SET FOCUS hDlg2, 210

                    DIALOG END hDlg4, 1 '''
                    FUNCTION = 1
                    END FUNCTION

                    The above is where the program exits with error. Below is summary of what the sample does:

                    1. PBMAIN : here can start the program and exit the program with no errors. Here is where I have DIALOG END CBHNDL when I end program

                    2. From PBMAIN go to ADDRESS dialog . Can exit here back to PBMAIN and exit from there , NO errors .

                    3. From PBMAIN to ADDRESS and to BROWSE . Exit browse, exit address, exit pbmain no error.

                    4. From PBMAIN to ADDRESS to BROWSE and to BROWSE PRINT PREVIEW. Here when exit Browse print preview and get error 'program has stopped working"

                    I am opening pbmain then hiding to open address. Then end address dialog to go to browse. And disable browse to goto browse print preview. exit here error. Hope that explains the sample code a little better. Going to check links shown and Pierre's code now.

                    [i am editing because usually when posting a long reply, I am kicked out forum and have to log in again, but before I log in again I copy all my text reply. then when I log in again I paste my text which come all jumbled up with no spacing and have to fix it. hope one day you get more time to reply before being kick out of fourm. need fix ]
                    Last edited by Robert Alvarez; 14 Jul 2017, 05:46 PM.
                    Robert

                    Comment


                    • #11
                      Hi Robert,
                      Looks like you posted to the wrong Thread Need to cut n paste over to here

                      Hi John,

                      Using msgbox to monitor a program's progress can be disruptive (especially in a loop or callback procedure).
                      A better option can be to use DIALOG SET TEXT sMsg +" - "+ Time$ or WINBEEP to keep track.
                      The latest compilers also have TXT.PRINT to a TXT.WINDOW..
                      Rgds, Dave

                      Comment


                      • #12
                        Thanks again, Dave. I do not use the code to interrupt a for/next loop in the finished product. I usually use it to see if I have picked up all/correct items in an array, just when testing a new piece of a program.

                        John Tate

                        Comment


                        • #13
                          Intrigued by what Dave posted in message #3, I tried to reduce it to a generic FUNCTION, but I ran into syntax problems (like trying to execute an EXIT in the function, where it was not part of the main loop).

                          After a couple of "think, think" moments, this "bear of little brains" remembered PB's macro capability. You can't see from this example, but I learned a lot from the MANY constructions I tried that were NOT successful (and are NOT posted)!

                          I have rarely used macros, but they seem to fit the bill perfectly for this!

                          -John


                          Code:
                          'Test mESC.bas    by jhm
                          
                          #Compile Exe
                          #Register None
                          #Dim All
                          
                          #Include Once "win32api.inc"
                          
                          'based on code from Dave Biggs (should be good for every type of PB EXIT!)
                          '
                          Macro mEsc(sType)
                             GetAsyncKeyState(%VK_ESCAPE)  ' Clear any previous notification
                             Sleep 50
                             If GetAsyncKeyState(%VK_ESCAPE) And &H0001 Then Exit sType
                          End Macro
                          
                          
                          Function PBMain
                             Local i As Quad
                             For i = 1 To 2000000
                                mEsc(For)                          '    <====  CUSTOMIZE THIS*
                             Next I
                          
                            ? "Counter = " & Format$(i)
                          
                          End Function
                          * Replace FOR with whatever loop type or structure you're wanting to break out of:
                          .......SELECT, TRY, MACRO, METHOD, PROPERTY, SUB, FUNCTION, etc.
                          Last edited by John Montenigro; 26 Jul 2017, 07:48 PM.

                          Comment


                          • #14
                            Interesting John,

                            Try this variation on your test code..
                            Code:
                            'Test mESC.bas    by jhm
                            
                            #Compile Exe
                            #Register None
                            #Dim All
                            
                            #Include Once "win32api.inc"
                            
                            'based on code from Dave Biggs (should be good for every type of PB EXIT!)
                            '
                            Macro mEsc(sType)
                             '   GetAsyncKeyState(%VK_ESCAPE)  ' Clear any previous notification
                               Sleep 50
                               If GetAsyncKeyState(%VK_ESCAPE) And &H0001 Then Exit sType      ' sType ignored?
                            End Macro
                            
                            
                            Function PBMain
                               Local i As Quad
                             ' '   GetAsyncKeyState(%VK_ESCAPE)     ' Removes notification from possible keypress prior to app running
                            
                               For i = 1 To 2000000
                                  mEsc()                          '    <====  CUSTOMIZE THIS*
                                  Sleep 200
                               Next I
                               Do
                                  mEsc()
                                  WinBeep 800,200
                               loop
                            
                              ? "Counter = " & Format$(i), %MB_SYSTEMMODAL,""
                            
                            End Function
                            BTW The reason for
                            ...GetAsyncKeyState(%VK_ESCAPE) ' Clear any previous notification ***
                            was to clear any remnant of an Esc key press lingering in the system from activity before the our app was run
                            .
                            Try Compiling the app, Press Escape, then Run the app to see how that works with and without the *** lines commented out.

                            Rgds, Dave

                            Comment

                            Working...
                            X