Results 1 to 3 of 3
  1. #1
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Interrupt Process (VB 6.0 / W98)

    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.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Interrupt Process (VB 6.0 / W98)

    Hello Chuck,

    I assume your application has a form.
    <UL><LI>Declare a module level variable in the module behind the form, say
    Dim intCount As Integer
    <LI>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.
    <LI>Be sure to include a DoEvents instruction somewhere within your loop. Otherwise, Windows won't have time to handle keystrokes.
    <LI>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.
    <LI>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]

  3. #3
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Interrupt Process (VB 6.0 / W98)

    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. <img src=/S/bow.gif border=0 alt=bow width=15 height=15>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •