The new vbAccelerator Site - more VB and .NET Code and Controls

Get the state of a key at any time


Steve McMahon(





Other Tips
All Tips
By Date
By Subject

API (33)
Manipulation (3)

Clipboard (3)
Box (5)

Desktop (3)
GDI (13)
Graphics (13)
Internet (2)
Comms (3)

Keyboard (2)
Mouse (1)
Shell (1)
Sprites (1)
Subclassing (3)
Box (2)

Windows (11)
Controls (10)


If you're trying to create an interactive app, or a VB game, whether its in DirectX or using GDI calls, then reading a key state asynchronously is considerably faster than waiting for KeyUp, KeyDown or KeyPress event on a form. Checking the key state this way can also be very useful when trying to check for shift states when responding to a WM_KEYDOWN event if you are doing sub-classing.

I used this technique to speed up a DirectX sample at Visual Basic Area 51. Because the original sample only updated the screen in response to form KeyDown events, the result was 400% quicker! (In fact, too quick - I had to put a slowdown in the loop to keep up!).

Create a new project, and add a Picture Box and a Command Button to the form. Then paste the following code into the form:

Private m_bPlay As Boolean
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Sub cmdPlay_Click()
Dim i As Long
Dim iLast As Long

&nbsp &nbsp If cmdPlay.Caption = "&Stop" Then
&nbsp &nbsp &nbsp &nbsp m_bPlay = False
&nbsp &nbsp &nbsp &nbsp cmdPlay.Caption = "&Play"
&nbsp &nbsp Else
&nbsp &nbsp &nbsp &nbsp cmdPlay.Caption = "&Stop"
&nbsp &nbsp &nbsp &nbsp m_bPlay = True
&nbsp &nbsp &nbsp &nbsp i = 1
&nbsp &nbsp &nbsp &nbsp Do
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
' Determine if the left or right keys are
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp ' pressed:
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp If (GetAsyncKeyState(vbKeyLeft)) Then
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
' Diminish the colour
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp i = i - 1
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp ElseIf (GetAsyncKeyState(vbKeyRight)) Then
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
' Increase the colour
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp i = i + 1
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp End If
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
' Colour within bounds:
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp If (i &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp If (i > 15) Then i = 1
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
' If colour has changed, change the display:
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp If (iLast i) Then
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp With Picture1
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp .Cls
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp .ForeColor = QBColor(i)
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
' Generate a RGB complement for the background:
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp .BackColor = &HFFFFFF And (Not QBColor(i))
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp .CurrentX = 64 * Screen.TwipsPerPixelX
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp .CurrentY = 64 * Screen.TwipsPerPixelY
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp Picture1.Print Hex$(QBColor(i))
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp End With
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp End If
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp iLast = i
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
' This is here to stop the animation
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp ' getting too fast to see:
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp Sleep 25
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
' Ensure we can still click buttons etc
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp DoEvents
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
&nbsp &nbsp &nbsp &nbsp Loop While m_bPlay
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
&nbsp &nbsp End If
&nbsp &nbsp
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
&nbsp &nbsp If (cmdPlay.Caption = "&Stop") Then
&nbsp &nbsp &nbsp &nbsp cmdPlay_Click
&nbsp &nbsp End If
End Sub

When you click on the command button, the code starts a DoEvents loop in which it checks if the Right Key or Left Key are pressed. When one is pressed, it changes the background and foreground colours of the Picture Box and displays the QBColor code.


Related Tips and Articles:


AboutContributeSend FeedbackPrivacy

Copyright 1998-1999, Steve McMahon ( All Rights Reserved.
Last updated: 01/08/98