• Interrupt Process (VB 6.0 / W98)

    Author
    Topic
    #371012

    I am an amatuer when it comes to VB, but I’m learning while trying to develop an appliction that involves an interrupt.

    For example, during the period a For/Next routine is running I need to count the number of times a key (say the space bar) is pressed. Obviously, this action must interrupt the running routine long enuf to bump a counter. I have no clue how to do this, so suggestions are very welcome.

    Viewing 0 reply threads
    Author
    Replies
    • #588557

      Hello Chuck,

      I assume your application has a form.

      • Declare a module level variable in the module behind the form, say
        Dim intCount As Integer
      • Set the KeyPreview property of the form to True. This means that the form gets to inspect all key events before the controls on the form.
      • Be sure to include a DoEvents instruction somewhere within your loop. Otherwise, Windows won’t have time to handle keystrokes.
      • Have code like the following behind the form:

        Private Sub Form_KeyPress(KeyAscii As Integer)
        Select Case KeyAscii
        Case vbKeySpace
        intCount = intCount + 1
        KeyAscii = 0
        End Select
        End Sub

        This increase the count variable every time the space bar is pressed.
        The instruction KeyAscii = 0 means that the keystroke won’t be processed by the control in which the user types.
        Remove this instruction if you want the keystroke to be processed.
        If you want to react to other keystrokes, add Case statements for them.

      • Be aware of the following:
        If a command button has the focus while the loop is being executed, any keystroke will act as a button click, even if KeyPreview is set to True.
        Depending on your application, you might need to take care of this.
        [/list]
      • #588831

        Hans,

        Your suggestion works just as you said. I changed the focus to another control prior to starting my For / Next loop, and that seems to work just fine.

        Thanks for the help. bow

    Viewing 0 reply threads
    Reply To: Interrupt Process (VB 6.0 / W98)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: