From f57055fd8c797fd49246541a7ffdd93bf0a4eed8 Mon Sep 17 00:00:00 2001 From: FunkyFr3sh Date: Wed, 20 Mar 2024 21:31:05 +0100 Subject: [PATCH] add better forground window detection and always enable new GetKeyState hooks --- inc/utils.h | 1 + src/utils.c | 7 +++++++ src/winapi_hooks.c | 10 ++++------ 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/inc/utils.h b/inc/utils.h index 2e2e779..253ec11 100644 --- a/inc/utils.h +++ b/inc/utils.h @@ -8,6 +8,7 @@ HMODULE WINAPI util_enumerate_modules(_In_opt_ HMODULE hModuleLast); BOOL util_is_bad_read_ptr(void* p); BOOL util_is_minimized(HWND hwnd); +BOOL util_in_foreground(); BOOL util_is_avx_supported(); void util_limit_game_ticks(); void util_update_bnet_pos(int newX, int newY); diff --git a/src/utils.c b/src/utils.c index 91fbcaf..d3b26c9 100644 --- a/src/utils.c +++ b/src/utils.c @@ -100,6 +100,13 @@ BOOL util_is_minimized(HWND hwnd) return IsIconic(hwnd) || (real_GetClientRect(hwnd, &rc) && (rc.right - rc.left == 0 || rc.bottom - rc.top == 0)); } +BOOL util_in_foreground() +{ + DWORD process_id = 0; + + return GetWindowThreadProcessId(real_GetForegroundWindow(), &process_id) && process_id == GetCurrentProcessId(); +} + BOOL util_is_avx_supported() { const DWORD XMM_STATE_BIT = 1 << 1; diff --git a/src/winapi_hooks.c b/src/winapi_hooks.c index 3d7632b..10c287e 100644 --- a/src/winapi_hooks.c +++ b/src/winapi_hooks.c @@ -718,10 +718,9 @@ BOOL WINAPI fake_PeekMessageA(LPMSG lpMsg, HWND hWnd, UINT wMsgFilterMin, UINT w SHORT WINAPI fake_GetKeyState(int nVirtKey) { - if (g_config.infantryhack && g_ddraw && g_ddraw->hwnd) + if (g_config.windowed && g_ddraw && g_ddraw->hwnd && !util_in_foreground()) { - if (real_GetForegroundWindow() != g_ddraw->hwnd) - return 0; + return 0; } return real_GetKeyState(nVirtKey); @@ -729,10 +728,9 @@ SHORT WINAPI fake_GetKeyState(int nVirtKey) SHORT WINAPI fake_GetAsyncKeyState(int vKey) { - if (g_config.infantryhack && g_ddraw && g_ddraw->hwnd) + if (g_config.windowed && g_ddraw && g_ddraw->hwnd && !util_in_foreground()) { - if (real_GetForegroundWindow() != g_ddraw->hwnd) - return 0; + return 0; } return real_GetAsyncKeyState(vKey);