To monitor all keys pressed any time (even when your form isn't active) using a GlobalHook you must put the HookProc in a DLL file which Vb can't make.C++ can. As an alternative you can use this code.
Code:
'in a module
Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Function GetPressedKey() As String
For Cnt = 32 To 128 'or 32 to 256
'Get the keystate of a specified key
If GetAsyncKeyState(Cnt) <> 0 Then
GetPressedKey = Chr$(Cnt)
Exit For
End If
Next Cnt
End Function
Sub TimerProc(ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long)
Ret = GetPressedKey
If Ret <> sOld Then
sOld = Ret
sSave = sSave + sOld
End If
'sSave contains pressed keys
End Sub
In a form
Code:
Private Sub Form_Load()
SetTimer Me.hwnd, 0, 1, AddressOf TimerProc
End Sub
Private Sub Form_Unload(Cancel As Integer)
KillTimer Me.hwnd, 0
End Sub
[Edited by Vlatko on 10-29-2000 at 03:56 PM]