'This vector contains the state of all keys
'I think KeyCode goes from 0 to 255, but you
'can change this if you want
Dim Keys(255) As Boolean
'Its used to stop the Do-Loop of the Form_Load
Dim StopLoop As Boolean
Private Sub Form_KeyDown(KeyCode As Integer, Shift As _
Integer)
'The key KeyCode is pressed now...
Keys(KeyCode) = True
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
'The key KeyCode is NOT pressed now...
Keys(KeyCode) = False
End Sub
Private Sub Form_Load()
Dim str As String
Me.Show
'The form must handle the key events
Me.KeyPreview = True
StopLoop = False
Do
'Just check for the arrow keys to see
'if they are pressed. To check form more
'keys, just ask for the correct subindex
'in the "Keys" array
str = ""
If Keys(vbKeyUp) Then str = str + "UP" & vbCrLf
If Keys(vbKeyDown) Then str = str + "DOWN" & vbCrLf
If Keys(vbKeyLeft) Then str = str + "LEFT" & vbCrLf
If Keys(vbKeyRight) Then str = str + "RIGHT" & vbCrLf
'Refresh the label
lblKeys = str
'Important!! (to not freeze the thing)
DoEvents
Loop Until StopLoop
End Sub
Private Sub Form_Unload(Cancel As Integer)
'Important!! To stop the loop, so the
'form can be unloaded
StopLoop = True
End Sub
|