EDIT FOR THOSE FOLLOWING COMMENTS:
https://learn.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-setwindowshookexa
HHOOK SetWindowsHookExA(
int idHook,
HOOKPROC lpfn,
HINSTANCE hmod,
DWORD dwThreadId
);
lpfn
Type: HOOKPROC
A pointer to the hook procedure. If the dwThreadId parameter is zero or specifies the identifier of a thread created by a different process, the lpfn parameter must point to a hook procedure in a DLL. Otherwise, lpfn can point to a hook procedure in the code associated with the current process.
hmod
Type: HINSTANCE
A handle to the DLL containing the hook procedure pointed to by the lpfn parameter. The hMod parameter must be set to NULL if the dwThreadId parameter specifies a thread created by the current process and if the hook procedure is within the code associated with the current process.
I do not understand why this does not work. For this version I do not block any hooks. I simply make entry into txt file noting time that message was processed by my keyboard callback function. However when I step through my main program, the hook catches OK. But KeyboardCallBack never gets executed, I think, as my log is empty safe for its initial header.
CONSTANTS AND APIS:
Private Declare PtrSafe Function SetWindowsHookEx Lib "user32" _
Alias "SetWindowsHookExA" (ByVal idHook As Long, _
ByVal lpfn As Long, _
ByVal hmod As Long, _
ByVal dwThreadId As Long) As Long
Private Declare PtrSafe Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(pDest As Any, _
pSource As Any, _
ByVal cb As Long)
Private Declare PtrSafe Function CallNextHookEx Lib "user32" _
(ByVal hHook As Long, _
ByVal nCode As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Private Type KBDLLHOOKSTRUCT
vkCode As Long
scanCode As Long
flags As Long
time As Long
dwExtraInfo As Long
End Type
Private Const HC_ACTION = 0
Private Const WH_KEYBOARD_LL = 13&
Private KeyboardHandle As Long
THE HOOK:
Public Sub HookKeyboard()
KeyboardHandle = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf KeyboardCallback, Application.Hinstance, 0)
End Sub
KEYBOARD CALLBACK AND AUXILIARY FUNCTIONS:
Public Function KeyboardCallback(ByVal Code As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Call TimeStamp
Static Hookstruct As KBDLLHOOKSTRUCT
If Code = HC_ACTION Then
Call CopyMemory(Hookstruct, ByVal lParam, Len(Hookstruct))
If BlockKey(Hookstruct) = True Then
KeyboardCallback = 1
Exit Function
End If
End If
KeyboardCallback = CallNextHookEx(KeyboardHandle, _
Code, wParam, lParam)
End Function
Private Function BlockKey(ByRef Hookstruct As KBDLLHOOKSTRUCT) As Boolean
BlockKey = False
End Function
Private Function TimeStamp()
Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Dim TSO As Object
Set TSO = FSO.OpenTextFile("C:\Users\evanm\Desktop\New Text Document.txt", 1)
Dim FileString As String
FileString = TSO.ReadAll
TSO.Close
FileString = FileString & " " & Now() & " "
Set TSO = FSO.OpenTextFile("C:\Users\evanm\Desktop\New Text Document.txt", 2)
TSO.WriteLine FileString
TSO.Close
Set FSO = Nothing
Set TSO = Nothing
End Function
TESTS:
Sub testHook()
Hook.HookKeyboard
Stop
'write a bunch of stuff
Stop
Hook.UnhookKeyboard
End Sub
Sub testWriteFile()
Hook.TimeStamp
End Sub