Set the KeyPreview property for the form to TRUE and you can then see every key press on the form using a Form_KeyUp, Form_KeyDown or Form_KeyPress event regardless of which control has the focus.
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
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
StrongM, that code works great, but it's only half of what I'm trying to do. How would I then replace the key with a different piece of text and have that feed into the active window, or is that asking a bit much?
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.