From 04efd4caf1f7de2d7ce475318fbc8d22ad029c64 Mon Sep 17 00:00:00 2001 From: Brues <5278969+brues-code@users.noreply.github.com> Date: Thu, 16 Jul 2026 20:18:43 -0500 Subject: [PATCH] input: reconcile modifier state on focus regain MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit MODIFIER_STATE_CHANGED never fired for a modifier released while WoW was in the background: WM_KEY* messages are only delivered to the focused window, so the WH_GETMESSAGE hook never saw the release and the cached bit stayed stuck down (also leaving IsLeft/RightShiftKeyDown etc. wrong). Add a WH_CALLWNDPROC thread hook to catch WM_ACTIVATEAPP (sent, not posted, so invisible to WH_GETMESSAGE). On app activation, resync the cached 6-bit mask from GetAsyncKeyState (global physical key state, unaffected by focus), firing MODIFIER_STATE_CHANGED for any bit that drifted while WoW was away. Both the keyboard path and the resync now funnel through a shared SetBit helper so the "only transitions fire" invariant holds regardless of source. Caveat (documented): the engine's native IsShiftKeyDown/IsControlKeyDown/ IsAltKeyDown bottom out at synchronous GetKeyState and share the blind spot; they are not reconciled here — only ClassicAPI's L/R functions and the event reflect the corrected state. --- docs/API.md | 25 ++++++++++++ src/input/Modifier.cpp | 87 +++++++++++++++++++++++++++++++++--------- 2 files changed, 93 insertions(+), 19 deletions(-) diff --git a/docs/API.md b/docs/API.md index 89c135c0..199943c4 100644 --- a/docs/API.md +++ b/docs/API.md @@ -2456,6 +2456,12 @@ Fires on every modifier-key press and release with `(key, down)`: Only transitions fire — key autorepeat does not. Matches 2.4.3+ semantics. +Releases that happen while WoW is **not the focused window** are also +handled: keyboard messages only reach WoW while it has focus, so a +modifier let go in the background is never seen live. On regaining focus +the cached state is reconciled against the true physical key state and +any missed transition fires then (see the focus-regain note below). + ```lua local f = CreateFrame("Frame") f:RegisterEvent("MODIFIER_STATE_CHANGED") @@ -2486,6 +2492,25 @@ renderer-state changes that recreate WoW's main window (e.g. toggling vertical sync), where an `SetWindowLongPtr`-style `WNDPROC` subclass would be left dangling. +**Focus-regain reconciliation.** `WM_KEY*` messages are only delivered +while WoW is the foreground window, so a modifier released while WoW is +in the background (alt-tabbed away) produces no message — the cached bit +would stay stuck down and the release event would never fire. A second +thread hook (`WH_CALLWNDPROC`, needed because `WM_ACTIVATEAPP` is *sent* +rather than *posted* and so is invisible to `WH_GETMESSAGE`) watches for +app activation; on regaining focus it polls `GetAsyncKeyState` for each +L/R modifier — which reads global physical key state regardless of focus +— and fires `MODIFIER_STATE_CHANGED` for any bit that changed while WoW +was away. So the release surfaces the moment you tab back, and +`IsLeft/RightShiftKeyDown` etc. read correct state immediately. + +> Note: the engine's own `IsShiftKeyDown` / `IsControlKeyDown` / +> `IsAltKeyDown` bottom out at the synchronous `GetKeyState`, which has +> the same background-release blind spot and is *not* reconciled by this +> hook — only ClassicAPI's L/R query functions and the event reflect the +> corrected state. Prefer `IsModifierKeyDown` / the L/R functions if you +> need the un-stuck value. + ### `NAME_PLATE_CREATED` / `NAME_PLATE_UNIT_ADDED` / `NAME_PLATE_UNIT_REMOVED` events Fire when nameplate state actually changes. Payloads: diff --git a/src/input/Modifier.cpp b/src/input/Modifier.cpp index dc469b80..eecebe97 100644 --- a/src/input/Modifier.cpp +++ b/src/input/Modifier.cpp @@ -63,6 +63,14 @@ const char *const kKeyName[6] = { "LSHIFT", "RSHIFT", "LCTRL", "RCTRL", "LALT", "RALT", }; +// L/R-specific virtual keys, one per bit, for the focus-regain resync. +// `GetAsyncKeyState` accepts the L/R-distinguishing VKs directly (unlike +// the keyboard-message path, where `VK_SHIFT` arrives merged and has to be +// disambiguated via the scancode). +const int kBitVk[6] = { + VK_LSHIFT, VK_RSHIFT, VK_LCONTROL, VK_RCONTROL, VK_LMENU, VK_RMENU, +}; + constexpr const char *kModifierStateChanged = "MODIFIER_STATE_CHANGED"; // Static-init reservation — runs before DllMain. The @@ -72,6 +80,7 @@ const Event::Custom::AutoReserve _reserveModifierStateChanged{kModifierStateChan uint32_t g_modifierMask = 0; HHOOK g_msgHook = nullptr; +HHOOK g_wndProcHook = nullptr; void PushDown(void *L, bool down) { if (down) @@ -80,6 +89,28 @@ void PushDown(void *L, bool down) { Game::Lua::PushNil(L); } +// Apply a single modifier bit transition: update the cached mask and, on a +// genuine change, fire `MODIFIER_STATE_CHANGED`. Both the keyboard-message +// path and the focus-regain resync funnel through here so the "only +// transitions fire" invariant holds regardless of source. +void SetBit(int bitIdx, bool down) { + const uint32_t bit = 1u << bitIdx; + const bool wasDown = (g_modifierMask & bit) != 0; + if (down == wasDown) + return; + + if (down) g_modifierMask |= bit; + else g_modifierMask &= ~bit; + + // Slot was assigned when the engine populated its event table; our + // hook on `RebuildEventTable` recorded the index. Lookup at fire + // time rather than caching because `/reload` re-runs the rebuild + // and the index may shift. + const int slot = Event::Custom::Lookup(kModifierStateChanged); + if (slot >= 0) + Event::Custom::Fire(slot, "%s%d", kKeyName[bitIdx], static_cast(down)); +} + template int __fastcall Script_IsKeyDown(void *L) { const bool down = (g_modifierMask & (1u << BitIdx)) != 0; @@ -131,23 +162,22 @@ void ProcessKeyMessage(UINT msg, WPARAM wParam, LPARAM lParam) { const int bitIdx = VkToBitIdx(wParam, lParam); if (bitIdx < 0) return; - - const uint32_t bit = 1u << bitIdx; - const bool wasDown = (g_modifierMask & bit) != 0; const bool nowDown = (msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN); - if (nowDown == wasDown) - return; + SetBit(bitIdx, nowDown); +} - if (nowDown) g_modifierMask |= bit; - else g_modifierMask &= ~bit; - - // Slot was assigned when the engine populated its event table; our - // hook on `RebuildEventTable` recorded the index. Lookup at fire - // time rather than caching because `/reload` re-runs the rebuild - // and the index may shift. - const int slot = Event::Custom::Lookup(kModifierStateChanged); - if (slot >= 0) - Event::Custom::Fire(slot, "%s%d", kKeyName[bitIdx], static_cast(nowDown)); +// Reconcile the cached mask with the true physical key state. Needed on +// focus regain: keyboard messages only arrive while WoW has focus, so a +// modifier released while WoW was in the background is never seen by the +// `WH_GETMESSAGE` hook and the bit stays stuck down (and the release event +// never fires). `GetAsyncKeyState` reads global async keyboard state +// regardless of focus, so polling it on activation recovers the missed +// transitions — `SetBit` fires `MODIFIER_STATE_CHANGED` for each drift. +void ResyncFromPhysicalState() { + for (int b = 0; b < 6; ++b) { + const bool down = (GetAsyncKeyState(kBitVk[b]) & 0x8000) != 0; + SetBit(b, down); + } } LRESULT CALLBACK GetMsgHook(int code, WPARAM wParam, LPARAM lParam) { @@ -164,11 +194,30 @@ LRESULT CALLBACK GetMsgHook(int code, WPARAM wParam, LPARAM lParam) { return CallNextHookEx(nullptr, code, wParam, lParam); } +// `WM_ACTIVATEAPP` is *sent* (via the window manager), not *posted* to the +// queue, so `WH_GETMESSAGE` never sees it — a `WH_CALLWNDPROC` hook catches +// sent messages before the target WNDPROC runs. On app activation +// (`wParam != FALSE`) we resync from physical key state to recover any +// modifier released while WoW was in the background. Like the message hook, +// this is thread-level (not per-`HWND`), so it survives window recreation. +LRESULT CALLBACK CallWndProcHook(int code, WPARAM wParam, LPARAM lParam) { + if (code == HC_ACTION) { + const CWPSTRUCT *cwp = reinterpret_cast(lParam); + if (cwp->message == WM_ACTIVATEAPP && cwp->wParam != FALSE) + ResyncFromPhysicalState(); + } + return CallNextHookEx(nullptr, code, wParam, lParam); +} + void InstallHook() { - if (g_msgHook != nullptr) - return; - g_msgHook = SetWindowsHookExA(WH_GETMESSAGE, &GetMsgHook, nullptr, - GetCurrentThreadId()); + if (g_msgHook == nullptr) { + g_msgHook = SetWindowsHookExA(WH_GETMESSAGE, &GetMsgHook, nullptr, + GetCurrentThreadId()); + } + if (g_wndProcHook == nullptr) { + g_wndProcHook = SetWindowsHookExA(WH_CALLWNDPROC, &CallWndProcHook, + nullptr, GetCurrentThreadId()); + } } void RegisterLuaFunctions() {