this works:
Code: Select all
hook1 := Hook.SetWinEventHook("EVENT_SYSTEM_FOREGROUND", "EVENT_SYSTEM_FOREGROUND", 0
, "WinProcCallback" ; <------ the function is not inside of a class
, 0, 0, "WINEVENT_OUTOFCONTEXT")
return
When i try to use the same function, but now the function is inside of the class Hook.WinProcCallback
something goes wrong and the value of hWnd inside of Hook.WinProcCallback is always 0.
Code: Select all
hook1 := Hook.SetWinEventHook("EVENT_SYSTEM_FOREGROUND", "EVENT_SYSTEM_FOREGROUND", 0
, "Hook.WinProcCallback" ; <------ same function but now inside of a class, the script doesn't work properly
, 0, 0, "WINEVENT_OUTOFCONTEXT")
return
Code: Select all
WinProcCallback(hWinEventHook, event, hwnd, idObject, idChild, dwEventThread, dwmsEventTime) {
WinGetClass, WinClass, ahk_id %hWnd%
FileAppend, hWnd: %hWnd% WinClass: %WinClass% `n,*
}
Class Hook {
SetWinEventHook(eventMin, eventMax, hmodWinEventProc, lpfnWinEventProc, idProcess, idThread, dwflags) {
static EVENT_SYSTEM_FOREGROUND := 0x0003
; eventMin/eventMax check
If ( !%eventMin% || !%eventMax% )
Return 0
nCheck := DllCall( "CoInitialize", Ptr,0 )
DllCall( "SetLastError", UInt,nCheck ) ; SetLastError in case of success/error
If ( nCheck == E_INVALIDARG || nCheck == E_OUTOFMEMORY || nCheck == E_UNEXPECTED )
Return -1
If ( isFunc(lpfnWinEventProc) )
lpfnWinEventProc := RegisterCallback(lpfnWinEventProc)
hWinEventHook := DllCall( "SetWinEventHook", UInt, %eventMin%, UInt, %eventMax%, Ptr,hmodWinEventProc, Ptr,lpfnWinEventProc, UInt,idProcess, UInt,idThread, UInt, dwflags )
Return (hWinEventHook) ? hWinEventHook : 0
}
WinProcCallback(hWinEventHook, event, hwnd, idObject, idChild, dwEventThread, dwmsEventTime) {
; hWnd is returned wrong being always 0 ; <---------------------
WinGetClass, WinClass, ahk_id %hWnd%
FileAppend, hWnd: %hWnd% WinClass: %WinClass% `n,*
}
}