API Calls

and what you can do with them...

  Declare function GetAsyncKeyState Lib "User" (Byval vKey as integer) as integer

Two uses of this API call are :


1. Interruptable do loop

In some circumstances we may have a time consuming do loop that we want the user to be able to interrupt using a mouse click or key stroke. A common method of implementing this is to use DoEvents, but this can lead to all sorts of problems with re-entry back into the loop.

This API call can be included in the loop and can be set so that the code will respond only if a particular key is pressed or mouse click occurs. The hex code for the different keys can be found in the SDK help file that comes with VB under the 'Virtual keys' section.

  do                                              ' Do something - check esc key 
    if
    GetAsyncKeyState(&H1B) then                   ' Respond to key 
      exit do 
    end if 
  loop                                  


2. Command button with continuous action

Sometimes we want an action to occur while a command button is pressed, but stop when the button is released. The following code in put in the mouse down event.

  Dim currenttime! 
  Dim RepeatDelayTime! 
  RepeatDelayTime! = .2 
    Do Until (GetAsyncKeyState(&H1) = 0) And (GetAsyncKeyState (&H20)= 0) 
      currenttime = Timer                           ' Do something 
    Do Until Timer > currenttime+RepeatDelayTime!   ' &H1 = mouse &H20 = spacebar 
      If GetAsyncKeyState(&H1) = 0 And GetAsyncKeyState (&H20) = 0 Then 
	Exit Do 
    End If 
  Loop 
  Loop

Ross Garner spoke at Visual Basic Australia '95.


[HOME] [TABLE OF CONTENTS] [SEARCH]