Depends...
If you are talking about capturing any keypresses in an your own application then check out my Application_KeyPreview code in thread222-716589 (and check out some of the other interesting approaches in that thread).
If you are suggesting that you want to capture a keypress ona system-wide basis, well not all that long ago you'd have been told it couldn't be done with VB alone...however, here's my first stab at a solution (note the 'limitation' that it requires NT SP3+/W2000/XP/W2003): thread222-118564
I also posted a solution that should work for all versions of Windows, which used journalling. I can't seem to find that thread at the moment, however, so here it is again (without instructions):
[tt]
Option Explicit
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Public Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Public Const HC_ACTION = 0
Public Const WM_KEYDOWN = &H100
Public prevLowLevelKybd As Long
Private Type EVENTMSG
message As Long
paramL As Long
paramH As Long
time As Long
hwnd As Long
End Type
Private Const WH_JOURNALRECORD = 0
Private Const WH_JOURNALPLAYBACK = 1
Public Function JournalHookProc(ByVal ncode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim HookStruct As EVENTMSG
Dim msgwParam As Long
Dim msglParam As Long
Debug.Print ncode
If ncode = HC_ACTION Then
CopyMemory HookStruct, ByVal lParam, Len(HookStruct)
'Debug.Print Hex(wParam); " ";
'Debug.Print Hex(HookStruct.paramL)
If HookStruct.message = WM_KEYDOWN Then
' OK, we've now got a structure that we can save off to a file, and then
' we could play our file back through a WH_JOURNALPLAYBACK hook
msgwParam = HookStruct.paramL And &HFF ' Got the virtual key code
msglParam = HookStruct.paramL \ &H100
Form1.Text1.Text = Form1.Text1.Text + Chr(msgwParam)
End If
End If
JournalHookProc = CallNextHookEx(prevLowLevelKybd, ncode, wParam, ByVal lParam)
End Function
Public Sub ToggleHook(WantHook As Boolean)
If WantHook = True Then
prevLowLevelKybd = SetWindowsHookEx(WH_JOURNALRECORD, AddressOf JournalHookProc, App.hInstance, 0)
Else
UnhookWindowsHookEx prevLowLevelKybd
prevLowLevelKybd = 0
End If
End Sub