Get the state of a key at any time

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 

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

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) 
    If (cmdPlay.Caption = "&Stop") Then 
        cmdPlay_Click 
    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.