Announcement

Collapse
No announcement yet.

Exiting a DO/LOOP WITH CTRL-Z

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

  • Exiting a DO/LOOP WITH CTRL-Z

    I want to exit a DO/LOOP exclusively with CTRL-Z or other CTRL-character combination and then establish a criteria based on that input.

    If I hit another character I want to be able to identify that I did not hit CTRL-Z and remain in the loop using the IF-THEN condition.

    The code below works if I hit escape, but if I hit any other key, it still exits the DO/LOOP and also will not let me establish Criteria1.

    Are there any comments or examples for this application.
    #COMPILE EXE
    #DIM ALL

    FUNCTION PBMAIN () AS LONG

    SubroutineA:

    DO WHILE Condition1 = 0 AND INSTAT = 0 AND Condition2 < 4

    'If I hit a key other than escape it still kicks me out of the loop. I would like to use If-Then to keep me in the loop.

    LOOP
    ' Another problem is that any key takes me out of the loop but does not meet the INKEY$ criteria below unless it was $ESC.

    IF INKEY$ =$ESC OR Condition2>=4 THEN ' I would like to make this CTRL-Z versus $ESC so that it is more selective.


    Criteria1 = z

    ELSE
    Criteria1 = w
    END IF

    RETURN ' For SubroutineA

    END FUNCTION


  • #2
    Code:
    DO
     IF ASC(INKEY$) = 26 THEN EXIT LOOP 'Control-Z
     SLEEP 10
    LOOP

    Comment


    • #3
      Thanks for the quick reply. My list of ascii characters has 26 as <substitute>. Is there a reference for these CTRL characters?

      Comment


      • #4
        Here's a list
        Code:
        #Compile Exe        ' PBCC6
        #Dim All
        #Break On
        '-----------------/
        
        Function ControlCode(sKey As String) As String
         Function = Read$(Asc(sKey))
        
         Data 01  SOH   ^A, 02  STX   ^B, 03  ETX   ^C, 04  EOT   ^D, 05  ENQ   ^E, 06  ACK   ^F, 07  BEL   ^G, 08  BS    ^H
         Data 09  HT    ^I, 10  LF    ^J, 11  VT    ^K, 12  FF    ^L, 13  CR    ^M, 14  SO    ^N, 15  SI    ^O, 16  DLE   ^P
         Data 17  DC1   ^Q, 18  DC2   ^R, 19  DC3   ^S, 20  DC4   ^T, 21  NAK   ^U, 22  SYN   ^V, 23  ETB   ^W, 24  CAN   ^X
         Data 25  EM    ^Y, 26  SUB   ^Z, 27  ESC   ^[, 28  FS    ^\, 29  GS    ^], 30  RS    ^^, 31  US    ^_
        End Function
        '------------------/ControlCode
        
        Function PBMain () As Long
         Local sTemp As String
         Local nKey As Long
          Console Set Loc 100, 100
          Console Set Screen 36, 50
           ? "   Control Characters"
           ? "ASC Name  CtrlKey"
           For nKey = 1 To 31
             sTemp = ControlCode(Chr$(nKey))
             ? sTemp
           Next
           ?
           ? "Esc key to exit"
           WaitKey$($Esc)
        End Function
        '------------------/PBMain
        Rgds, Dave

        Comment


        • #5
          Is there a reference for these CTRL characters?
          https://en.wikipedia.org/wiki/ASCII
          Dale

          Comment


          • #6
            > Is there a reference for these CTRL characters?

            The basic Ctrl-keys are easy to remember... Ctrl-A = 1 through Ctrl-Z = 26, in alphabetical order.
            "Not my circus, not my monkeys."

            Comment


            • #7
              Not directly relevant here BUT in case you want to extend this logic to reading a file...

              CTRL+Z or CHAR$(26) is also recognized in some cases as "End of File" so if you are looping based on reading a file you need to keep that in mind.
              Michael Mattias
              Tal Systems (retired)
              Port Washington WI USA
              [email protected]
              http://www.talsystems.com

              Comment


              • #8
                Thanks for all of the replies.

                Comment


                • #9
                  You never know in the future what computer or keyboard layout you will be using.
                  Imagine a virtual operating system on an Apple computer. I believe all computers have the control key on them. They may not have a Break key on those keyboards. I believe I put a break loop using the Ctrl-C key but now I wish I used a different key. Maybe Ctrl-B or some other character because a Ctrl-C might break a batch process.
                  I also believe I used a counter in the loop to check for a keboard input on every x iterations rather than every single iteration. Something like this.
                  Incr x
                  If x = 50 then
                  If inkey equal key to check then exit loop
                  x=0
                  end If
                  p purvis

                  Comment


                  • #10
                    . I believe I put a break loop using the Ctrl-C key but now I wish I used a different key. Maybe Ctrl-B or some other character because a Ctrl-C might break a batch process.
                    PB/CC programs now offer the #BREAK ON|OFF (default OFF) compilation option to control if your batch program will/won't terminate on Ctrl+break.

                    FWIW Windows' programs NEVER terminated on CTRL+C unless specifically programmed to do so; however, executing MS-DOS Batch and Windows' Command files DO terminate on Ctrl+C. Before you curse "Ctrl+C" actions you need to be sure of where they occurred. (Ctrl+C is intercepted to "copy selected text to clipboard" under Windows.)
                    Michael Mattias
                    Tal Systems (retired)
                    Port Washington WI USA
                    [email protected]
                    http://www.talsystems.com

                    Comment


                    • #11
                      MCM
                      From memory. If you don't use #BREAK ON then the exe will be slower to load.
                      I suppose that one might be able to clear the keyboard buffer before exiting the program if that might solve for keeping a batch process from being aborted.
                      I placed in a loop for a file directory listing an abort with CRTL-C but when I redirect the output with a pipe to the operating program SORT. It seems I have trouble breaking out of the loop with CRTL-C, but I have tested that condition fullly. I am usually the only one who uses the program.
                      p purvis

                      Comment


                      • #12
                        From memory. If you don't use #BREAK ON then the exe will be slower to load.
                        If this is an issue because of the number of times the program is loaded ("shelled" or "launched" ) for each run of the "main" program then it's probably time to invest in a PB/Windows license and package the needed functions in a Dynamic Link Library.

                        placed in a loop for a file directory listing an abort with CRTL-C [and piping to SORT] .
                        Um, using .."DO .. DIR$.... LOOP" plus one of the many fine examples of sorting found on this board eliminates any need to place yourself at the mercy of the vagaries of the keyboard and the behavior of CTRL+C.

                        Just sayin', IMNSHO there is absolutely no need to live with MS-DOS issues and limitations twenty-plus years after the release of Win/32.
                        Michael Mattias
                        Tal Systems (retired)
                        Port Washington WI USA
                        [email protected]
                        http://www.talsystems.com

                        Comment

                        Working...
                        X