https://autohotkey.com/docs/commands/RegisterCallback.htm#Indirect
I don't understand why would would add or subtract A_PtrSize from the args.
What does this do?
Also, adding +0 is just for ensuring that it's purely numeric, and not a numeric string, right?
Also, why in this example the ParamCount parameter is set to 3, even though only 2 arguments are passed though?
Here is an other example of a cool systemwide hook script I found with a Google search:
Code: Select all
#Persistent
mh:=new mouseHook("h")
mh.hook()
h(h,x,y){
ToolTip, % "Mouse moved at " x A_Space y
}
class mouseHook{
; User methods
hook(){
if !this.hHook ; WH_MOUSE_LL := 14 hMod:=0 dwThreadId:=0
this.hHook:=DllCall("User32.dll\SetWindowsHookEx", "Int", 14, "Ptr", this.regCallBack:=RegisterCallback(this.LowLevelMouseProc,"",4, &this), "Uint", 0, "Uint", 0, "Ptr")
return
}
unHook(){
if this.hHook
DllCall("User32.dll\UnhookWindowsHookEx", "Uint", this.hHook)
return this.hHook:=""
}
; Internal methods.
__new(callbackFunc){
(this.callbackFunc:= isObject(callbackFunc) ? callbackFunc : isFunc(callbackFunc) ? func(callbackFunc) : "")
if !this.callbackFunc
throw exception("invalid callbackFunc")
}
LowLevelMouseProc(args*) {
; (nCode, wParam, lParam)
Critical
this:=Object(A_EventInfo)
nCode:=NumGet(args-A_PtrSize,"Int")
wParam:=NumGet(args+0,0,"Ptr")
lParam:=NumGet(args+0,A_PtrSize,"UPtr")
x:=NumGet(lParam+0,0,"Int")
y:=NumGet(lParam+0,4,"Int")
flags:=NumGet(lParam+0,12,"UInt")
this.callbackFunc.Call(flags,x,y)
return DllCall("User32.dll\CallNextHookEx","Uint",0, "Int", nCode,"Uptr", wParam,"Ptr",lParam)
}
__Delete(){
this.unHook()
if this.regCallBack
DllCall("GlobalFree", "Ptr", this.regCallBack)
return
}
}
esc::exitapp
With what logic is wParam at offset 0 and lParam at offset 4 or 8? And why are we subtracting A_PtrSize from the arg when getting the nCode?
Why is for example this not correct?
Code: Select all
;using WH_MOUSE_LL the args we get are:
; 32bit OS 64bit OS
;int nCode 0 0
;WPARAM wParam 4 8
;LPARAM lParam 8 16
nCode := NumGet(args+0, 0, "Int")
wParam := NumGet(args+0, A_PtrSize, "Ptr")
lParam := NumGet(args+0, 2*A_PtrSize, "UPtr")
Any help would be appreciated,
Thanks.