-
Jan 19th, 2021, 01:14 AM
#1
Thread Starter
Fanatic Member
WH_KEYBOARD_LL x64bit Windows 10 no longer works !
Trying to install a global keyboard hook.
The call to SetWindowsHookEx is successful as it returns a valid hook handle, however, the Keyboard Callback function never gets called whenever a key is pressed, not even in the current process ! No error is ever produced, just nothing happens !
The same code works well in x32 bit platforms.
Also, it is worth noting that a low level mouse hook (WH_MOUSE_LL) installed in the same fashion works fine in x64bit ... Only the WH_KEYBOARD_LL is the one that doesn't work !
What is going on ? Is this a known bug or something ?
Code:
Option Explicit
Type KBDLLHOOKSTRUCT
vkCode As Long
scanCode As Long
flags As Long
time As Long
dwExtraInfo As Long
End Type
Declare PtrSafe Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As LongPtr, ByVal hmod As LongPtr, ByVal dwThreadId As Long) As LongPtr
Declare PtrSafe Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As LongPtr) As Long
Declare PtrSafe Function CallNextHookEx Lib "user32" (ByVal hHook As LongPtr, ByVal ncode As Long, ByVal wParam As LongPtr, lParam As Any) As LongPtr
Declare PtrSafe Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As LongPtr
Dim KeyboardHandle As LongLong
Sub HookKeyboard()
Const WH_KEYBOARD_LL = 13&
If KeyboardHandle = 0 Then
KeyboardHandle = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf KeyboardCallback, GetModuleHandle(vbNullString), 0&)
Debug.Print "hHook = " & KeyboardHandle & _
vbCrLf & "Err.LastDllError = " & Err.LastDllError ' <== Success --Valid hook handle
End If
End Sub
Sub UnhookKeyboard()
If KeyboardHandle <> 0 Then
UnhookWindowsHookEx KeyboardHandle
KeyboardHandle = 0
End If
End Sub
Private Function KeyboardCallback(ByVal Code As Long, ByVal wParam As LongLong, lParam As KBDLLHOOKSTRUCT) As LongLong
Const HC_ACTION = 0&
If Code = HC_ACTION Then
Debug.Print "This never gets called !!!"
End If
KeyboardCallback = CallNextHookEx(ByVal KeyboardHandle, ByVal Code, ByVal wParam, ByVal lParam)
End Function
Last edited by JAAFAR; Jan 19th, 2021 at 01:25 AM.
-
Jan 19th, 2021, 02:58 AM
#2
Re: WH_KEYBOARD_LL x64bit Windows 10 no longer works !
removed
Edit: Oops just noticed you have code for VBA 7. (LongPtr)
Last edited by Krool; Jan 19th, 2021 at 03:16 AM.
-
Jan 19th, 2021, 03:48 AM
#3
Re: WH_KEYBOARD_LL x64bit Windows 10 no longer works !
Is your Office 32 or 64-bit?
EDIT: The Header/Parameters of the Callback-Function looks fishy to me.
i would have expected the standard Datatypes for wparam and lparam
Last edited by Zvoni; Jan 19th, 2021 at 03:54 AM.
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Jan 19th, 2021, 04:00 AM
#4
Thread Starter
Fanatic Member
Re: WH_KEYBOARD_LL x64bit Windows 10 no longer works !
Originally Posted by Zvoni
Is your Office 32 or 64-bit?
EDIT: The Header/Parameters of the Callback-Function looks fishy to me.
i would have expected the standard Datatypes for wparam and lparam
64bit, hence the LongLong pointers.
The callback params are declared correctly.. In fact, the entire code is correct. However, it doesn't work.
-
Jan 19th, 2021, 04:02 AM
#5
Re: WH_KEYBOARD_LL x64bit Windows 10 no longer works !
If it doesn't work, then the code is not correct.
And LongLong is not a pointer: It's a 64-Bit Integer
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Jan 19th, 2021, 04:15 AM
#6
Thread Starter
Fanatic Member
Re: WH_KEYBOARD_LL x64bit Windows 10 no longer works !
From the MS Documetation :
The hook procedure should process a message in less time than the data entry specified in the LowLevelHooksTimeout value in the following registry key:
HKEY_CURRENT_USER\Control Panel\Desktop
The value is in milliseconds. If the hook procedure times out, the system passes the message to the next hook. However, on Windows 7 and later, the hook is silently removed without being called. There is no way for the application to know whether the hook is removed.
So I went to the registry in case this was the reason for the code not working and checked it out but, I found no such reg key entry (LowLevelHooksTimeout) :
-
Jan 19th, 2021, 04:21 AM
#7
Thread Starter
Fanatic Member
Re: WH_KEYBOARD_LL x64bit Windows 10 no longer works !
Originally Posted by Zvoni
If it doesn't work, then the code is not correct.
And LongLong is not a pointer: It's a 64-Bit Integer
x64bit pointers are LongLong which are 64bit Long values or 64bit variables for storing memory address pointers and API object handles.:
Code:
Dim lPtr As LongLong
MsgBox LenB(lPtr) '<==< 8 Bytes = 64 bit
Last edited by JAAFAR; Jan 19th, 2021 at 04:25 AM.
-
Jan 19th, 2021, 01:09 PM
#8
Thread Starter
Fanatic Member
Re: WH_KEYBOARD_LL x64bit Windows 10 no longer works !
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|