How to exit a While loop when key pressed

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sohan
    New Member
    • May 2007
    • 3

    How to exit a While loop when key pressed

    hello !!! i am new in VB

    the program below is onli small part ,, at the moment i only want to put it into do while loop which can be stop using any keyboard button

    is there any way i acn do it ???

    thankxxxxxx

    [CODE=vb]While (temp_time >= 0)
    T1 = Timer
    DT = 0
    Stop_async1 = 0
    Half_ready = 0

    While (Half_ready = 0) 'And (Stop_async1 = 0)
    result = D2K_AI_AsyncDbl BufferHalfReady (Card_number, Half_ready, Stop_async1)
    If result < 0 Then
    box = MsgBox("ERROR", vbYesNo)
    End If
    Wend
    result = D2K_AI_AsyncDbl BufferToFile(Ca rd_number)
    'While (Stop_async1 = 0)
    ' result = D2K_AI_AsyncChe ck(Card_number, Stop_async1, Access_count)
    'Wend
    'result = D2K_AI_AsyncCle ar(Card_number, 0, Access_count)
    T2 = Timer
    DT = T2 - T1
    temp_time = temp_time - DT
    DT_temp = DT_temp + DT
    If DT_temp >= 1 Then
    DT_temp = 0
    Info_Label.Capt ion = "Remained time: " + Str$(temp_time) + " sec."
    END IF
    Wend[/CODE]
    Last edited by Killer42; May 25 '07, 02:11 AM. Reason: Added [CODE=vb] tag
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    That shouldn't be too hard. Just create a global or form-level boolean variable, and exit the loop if you find that the value is True.

    Then, on your form, set the .KeyPreview property to True, and in the KeyPress or KeyDown event procedure, set the boolean variable to True.

    This will work for VB6 - VB.Net is different in many ways, so may require a different technique. You didn't say which you're using.

    Comment

    • sohan
      New Member
      • May 2007
      • 3

      #3
      thx killer42 but i didnt quite catch u as i am totally lay man in VB , will that be possible for you to explain with simple example.

      i am using VB 6

      thanks a lot

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Originally posted by sohan
        thx killer42 but i didnt quite catch u as i am totally lay man in VB , will that be possible for you to explain with simple example.
        i am using VB 6
        Ok, here's a very simple program which demonstrates the concept.

        Create a new project. Add a form, go to the code window for the form, and paste in this code (replacing anything already there)...

        [CODE=vb]Option Explicit

        Private Sub Form_KeyPress(K eyAscii As Integer)
        UserWantsOut = True
        End Sub[/CODE]

        Add a code module, and paste this code into it...

        [CODE=vb]Option Explicit

        Public UserWantsOut As Boolean

        Sub Main()
        Dim Xlimit As Long, Ylimit As Long
        Load Form1
        DoEvents
        Xlimit = Form1.ScaleWidt h
        Ylimit = Form1.ScaleHeig ht
        Randomize
        Form1.Show
        Do Until UserWantsOut
        Form1.PSet (Rnd * Xlimit, Rnd * Ylimit), RGB(Rnd * 255, Rnd * 255, Rnd * 255)
        DoEvents
        Loop
        End Sub[/CODE]

        In the Project Properties, set the startup object to Sub Main. Then, when you run it, this should display the form and start filling it with randomly-coloured dots until you press any key, then stop.

        Be warned, though - if you use your mouse to close the window, then the keypress will never happen, so the loop will keep running forever (or until you tell VB to stop). After all, it's only a simple demo program. I suppose I should have set the UserWantsOut variable to True in the form's QueryUnload event procedure, but got a bit lazy.

        Comment

        • Helmer
          New Member
          • May 2007
          • 10

          #5
          hey if u can get a chart for the keycodes of the keys on a keyboard, im sure u are all familiar with a case statement...
          form keydown event

          select case Keycode

          case vbkeyX' x=keycode

          here u put wat u want t happen wen that key is hit

          case vbkeyY y=keycode

          here u put wat u want t happen wen that key is hit

          end select

          or more efficiently

          if keycode <>0 then
          ...
          end if

          for most keys the keycode is the keyname ex K's keycode is vbkeyK, etc

          Comment

          • sohan
            New Member
            • May 2007
            • 3

            #6
            thanks guys thanks a lot

            Comment

            • LacrosseB0ss
              New Member
              • Oct 2006
              • 112

              #7
              Originally posted by Helmer
              hey if u can get a chart for the keycodes of the keys on a keyboard, .....

              All this is is a simple ASCII chart. Type that in Google and you'll get umpteen bajillion hits (both web, and image). If you're looking for foreign language (not English) characters you'll need to check up "Extended ASCII Chart" but same deal.
              Last edited by LacrosseB0ss; May 29 '07, 04:00 PM. Reason: forgot the quote, oops!

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                Originally posted by LacrosseBoss
                All this is is a simple ASCII chart. Type that in Google and you'll get umpteen bajillion hits (both web, and image).
                Just look in your VB doco.

                Comment

                • LacrosseB0ss
                  New Member
                  • Oct 2006
                  • 112

                  #9
                  Originally posted by Killer42
                  Just look in your VB doco.
                  hmm, I didn't know it was there too. Thanks killer!

                  Comment

                  • Killer42
                    Recognized Expert Expert
                    • Oct 2006
                    • 8429

                    #10
                    Originally posted by LacrosseBoss
                    hmm, I didn't know it was there too. Thanks killer!
                    No prob. :)

                    From memory, I think the easiest way to track it down is to look up SendKeys.

                    Comment

                    Working...