Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

capture key 1

Status
Not open for further replies.

LordDuzy

Programmer
Feb 12, 2004
22
PL
what is the best and efective way to catch the key's that are typed on the keyboard, when the form is not on focus??

LordDuzy
 
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.
 
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
 
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?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top