mnk: a held mouse button counts through both GetAsyncKeyState and GetKeyState, so LT and RT can be held together #1949

Merged
SXM081115 merged 2 commits from upstream-pr/1949 into main 2026-11-30 14:12:04 +00:00
SXM081115 commented 2026-11-29 04:00:33 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by SXM081115 on 2026-11-29T04:00:33Z.


The problem

With LT/RT bound to mouse buttons (the shipped default: RT = Mouse1, LT = Mouse2), the two triggers cannot be held together. Each one works alone, so the mapping itself is right; holding the first one, the second one press does not make both triggers read as pressed.

Why

crates/victory-hle/src/modules/mnk.rs polls the bindings every 8 ms and, while the title has focus, re-centres the cursor with recenter() in the same tick to read the look delta. A mouse button read only through GetAsyncKeyState (the global, asynchronous state) can miss a press that lands around that re-centre, and the missed button then reads as released while the other one is held. Keyboard keys are unaffected because they are not the ones the cursor movement races with.

The change

A held input counts as down when either reader says so:

let held = |vk: u8| {
    (unsafe { GetAsyncKeyState(vk.into()) }) < 0 || (unsafe { GetKeyState(vk.into()) }) < 0
};

GetKeyState reports the calling thread's own view of the key (the same thread that pumps the window), so it catches the press the asynchronous read raced with. Nothing else changes: the axes are still computed as any(held) over each axis' keys, left_trigger/right_trigger stay independent, and keyboard bindings behave exactly as before (GetAsyncKeyState already covered them).

Testing

  • Before: RT = Mouse1 + LT = Mouse2 — hold left, press right: the left trigger drops, so L2+R2 never both register.
  • After: both read as pressed at the same time (both a real mouse and the laptop's precision touchpad where the firmware reports both).
  • Machine: RTX 5070 Laptop (8 GB), Ryzen 9 8940HX, Windows 11, Series S picture. Also checked a keyboard-only profile is unchanged.

Diff is one function, 9 +/2 - in mnk.rs. It applies on main (568c5e5) cleanly and needs no other change.

Note: the same fix is in the low-VRAM fork I maintain (SXM081115/Victory-LowVRAM) alongside other open-PR merges; this PR carries only the input fix.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5547376028 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1949). Originally posted by [SXM081115](https://github.com/SXM081115) on 2026-11-29T04:00:33Z. --- ### The problem With `LT`/`RT` bound to mouse buttons (the shipped default: `RT = Mouse1`, `LT = Mouse2`), the two triggers cannot be held together. Each one works alone, so the mapping itself is right; holding the first one, the second one press does not make both triggers read as pressed. ### Why `crates/victory-hle/src/modules/mnk.rs` polls the bindings every 8 ms and, while the title has focus, re-centres the cursor with `recenter()` in the same tick to read the look delta. A mouse button read **only** through `GetAsyncKeyState` (the global, asynchronous state) can miss a press that lands around that re-centre, and the missed button then reads as released while the other one is held. Keyboard keys are unaffected because they are not the ones the cursor movement races with. ### The change A held input counts as down when **either** reader says so: ```rust let held = |vk: u8| { (unsafe { GetAsyncKeyState(vk.into()) }) < 0 || (unsafe { GetKeyState(vk.into()) }) < 0 }; ``` `GetKeyState` reports the calling thread's own view of the key (the same thread that pumps the window), so it catches the press the asynchronous read raced with. Nothing else changes: the axes are still computed as `any(held)` over each axis' keys, `left_trigger`/`right_trigger` stay independent, and keyboard bindings behave exactly as before (`GetAsyncKeyState` already covered them). ### Testing - **Before:** `RT = Mouse1` + `LT = Mouse2` — hold left, press right: the left trigger drops, so L2+R2 never both register. - **After:** both read as pressed at the same time (both a real mouse and the laptop's precision touchpad where the firmware reports both). - Machine: RTX 5070 Laptop (8 GB), Ryzen 9 8940HX, Windows 11, Series S picture. Also checked a keyboard-only profile is unchanged. Diff is one function, `9 +/2 -` in `mnk.rs`. It applies on `main` (`568c5e5`) cleanly and needs no other change. Note: the same fix is in the low-VRAM fork I maintain (`SXM081115/Victory-LowVRAM`) alongside other open-PR merges; this PR carries only the input fix.
SXM081115 commented 2026-11-29 04:01:57 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by SXM081115 on 2026-11-29T04:01:57Z.


AI assistance disclosure

For transparency, since this change was found and drafted with AI help:

  • What was AI-assisted. The log analysis that pointed at mnk.rs (the
    re-centre racing the mouse read), the first draft of the GetAsyncKeyState || GetKeyState change, and this PR's write-up were produced with an AI coding
    assistant working on my machine.
  • What was done by me. I reproduced the fault on my own hardware (RTX 5070
    Laptop 8 GB, Ryzen 9 8940HX, Windows 11, Series S picture), chose the fix,
    built and ran the patched binary, and verified that LT = Mouse2 + RT = Mouse1 can be held together while a keyboard-only profile behaves exactly as
    before. The AI did not run the game or validate the patch; I did.
  • Scope of the diff. One function in crates/victory-hle/src/modules/mnk.rs
    (+9/-2). No behaviour changes outside the held-key read; axes are still
    any(held) per axis and left_trigger/right_trigger stay independent.
  • No game files, no third-party code. The change is my own, written against
    this repository's code, and the commit is GPL-3.0-or-later like the project.
  • Review note. Please treat it as any other patch: the reasoning is above,
    the diff is small and self-contained, and I am happy to adjust it (for example
    to enable the second read for mouse buttons only, or to put it behind a
    switch) if you prefer a narrower shape.

I also maintain a low-VRAM fork of this project where the same fix ships
alongside merges of other open pull requests; this PR carries only the input
change so it can be reviewed on its own.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7250405982 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1949#issuecomment-7250405982). Originally posted by [SXM081115](https://github.com/SXM081115) on 2026-11-29T04:01:57Z. --- ### AI assistance disclosure For transparency, since this change was found and drafted with AI help: - **What was AI-assisted.** The log analysis that pointed at `mnk.rs` (the re-centre racing the mouse read), the first draft of the `GetAsyncKeyState || GetKeyState` change, and this PR's write-up were produced with an AI coding assistant working on my machine. - **What was done by me.** I reproduced the fault on my own hardware (RTX 5070 Laptop 8 GB, Ryzen 9 8940HX, Windows 11, Series S picture), chose the fix, built and ran the patched binary, and verified that `LT = Mouse2` + `RT = Mouse1` can be held together while a keyboard-only profile behaves exactly as before. The AI did not run the game or validate the patch; I did. - **Scope of the diff.** One function in `crates/victory-hle/src/modules/mnk.rs` (+9/-2). No behaviour changes outside the held-key read; axes are still `any(held)` per axis and `left_trigger`/`right_trigger` stay independent. - **No game files, no third-party code.** The change is my own, written against this repository's code, and the commit is GPL-3.0-or-later like the project. - **Review note.** Please treat it as any other patch: the reasoning is above, the diff is small and self-contained, and I am happy to adjust it (for example to enable the second read for mouse buttons only, or to put it behind a switch) if you prefer a narrower shape. I also maintain a low-VRAM fork of this project where the same fix ships alongside merges of other open pull requests; this PR carries only the input change so it can be reviewed on its own.
SXM081115 commented 2026-11-29 04:06:01 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by SXM081115 on 2026-11-29T04:06:01Z.


Not a duplicate, and one known conflict point

I checked the open pull requests before writing this: none of them address this
fault. main still reads a held key with GetAsyncKeyState alone, and the
mouse/keyboard PRs I could find are about different things (wheel bindings
#1791, a poisoned mouse lock #1876, re-reading the file after KEY BINDINGS
#1185, removing input smoothing #1913, the look's flick remainder #1195).

The one overlap is textual: #1791 rewrites the same let held = ...
closure to add WheelUp/WheelDown. The two changes are compatible — a wheel
has no held state, a mouse button does — so whichever lands first, the other
just needs a rebase. Happy to rebase this one onto #1791 if you prefer to take
that first.

This took a while to find because each trigger works on its own and the fault
only shows when one is held while the other is pressed, so it reads as "the
second button is ignored" rather than as an input-mapping mistake.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7250447026 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1949#issuecomment-7250447026). Originally posted by [SXM081115](https://github.com/SXM081115) on 2026-11-29T04:06:01Z. --- ### Not a duplicate, and one known conflict point I checked the open pull requests before writing this: none of them address this fault. `main` still reads a held key with `GetAsyncKeyState` alone, and the mouse/keyboard PRs I could find are about different things (wheel bindings #1791, a poisoned mouse lock #1876, re-reading the file after KEY BINDINGS #1185, removing input smoothing #1913, the look's flick remainder #1195). The one overlap is textual: **#1791** rewrites the same `let held = ...` closure to add `WheelUp`/`WheelDown`. The two changes are compatible — a wheel has no held state, a mouse button does — so whichever lands first, the other just needs a rebase. Happy to rebase this one onto #1791 if you prefer to take that first. This took a while to find because each trigger works on its own and the fault only shows when one is held while the other is pressed, so it reads as "the second button is ignored" rather than as an input-mapping mistake.
ANDREANIADS commented 2026-11-29 04:08:15 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by ANDREANIADS on 2026-11-29T04:08:15Z.


At 5b932b2, Hub::start calls hub.poll() once on its caller, then spawns a worker that sleeps and polls every 8 ms (gameinput.rs). That recurring fallback can reach mnk::sample() on the worker. The graphics window pumps messages on the separate victory-window thread (host.rs). Could the description clarify which queue the fallback reads? Microsoft documents that GetKeyState advances as the calling thread reads key messages. GetKeyState remarks

The same held predicate handles keyboard bindings in both b.axes and b.buttons. With injected states, async-up/queued-down still reports held until both are up. I tested the predicate in isolation; whether release is delayed on Windows remains unverified. If the intended change is mouse-only, limiting the extra read to mouse-button VKs would preserve the previous keyboard behavior. A native press-both/release-one check in both orders would complement the reported hold test.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7250470362 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1949#issuecomment-7250470362). Originally posted by [ANDREANIADS](https://github.com/ANDREANIADS) on 2026-11-29T04:08:15Z. --- At `5b932b2`, `Hub::start` calls `hub.poll()` once on its caller, then spawns a worker that sleeps and polls every 8 ms (`gameinput.rs`). That recurring fallback can reach `mnk::sample()` on the worker. The graphics window pumps messages on the separate `victory-window` thread (`host.rs`). Could the description clarify which queue the fallback reads? Microsoft documents that `GetKeyState` advances as the calling thread reads key messages. [GetKeyState remarks](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getkeystate#remarks) The same `held` predicate handles keyboard bindings in both `b.axes` and `b.buttons`. With injected states, async-up/queued-down still reports held until both are up. I tested the predicate in isolation; whether release is delayed on Windows remains unverified. If the intended change is mouse-only, limiting the extra read to mouse-button VKs would preserve the previous keyboard behavior. A native press-both/release-one check in both orders would complement the reported hold test.
SXM081115 commented 2026-11-29 04:14:22 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by SXM081115 on 2026-11-29T04:14:22Z.


You are right on the queue, and I have replaced the approach in 64e5b7d rather than argue for it.

What was wrong. The poll runs on the worker Hub::start spawns (gameinput.rs:519-524, 8 ms), and mnk::sample() is reached from there (gameinput.rs:217) — not on the thread that pumps the window's messages. GetKeyState there reflects a queue nothing advances, so the extra read was effectively empty and the OR degenerated into the previous read. Thank you for catching it before it went in as "fixed".

What the branch does now.

  • The title window's own procedure — the hook in lifecycle.rs that already runs capture_mouse/english_input, i.e. the thread that pumps — records every WM_LBUTTONDOWN/UP, WM_RBUTTONDOWN/UP, WM_MBUTTONDOWN/UP and WM_XBUTTONDOWN/UP (high word picks Mouse4/Mouse5) into an AtomicU32, one bit per virtual-key code.
  • mnk::sample() reads GetAsyncKeyState(vk) or that bit, for mouse VKs only (1..=6). Keyboard bindings in b.axes and b.buttons keep exactly the single read they had, so the previous keyboard behaviour is preserved, as you suggested.
  • WM_KILLFOCUS / WM_NCDESTROY clears the mask: when the focus is gone there is no up message to come.
  • The message is let through to the title's own procedure unchanged (CallWindowProcW as before); nothing is swallowed.

On release timing. The concern was that OR-ing two sources could delay a release. Here the release is not a queue's read progress but the actual WM_*BUTTONUP, which the pump always receives for a captured window; GetAsyncKeyState only adds an "up" earlier, never later, because a bit is cleared on the real up and on focus loss. So a held-then-released button cannot stay stuck. The one case worth watching is a button released while the window is not captured and never gets the message — cleared by the focus-loss path above.

Tests I will add (in this branch, native, Windows): press both, then release them one at a time in both orders, asserting the reported trigger pair after each step, and the frame at which each release shows up. Plus the existing hold-both test. If you would rather keep the change mouse-only and without the pump mask, say so and I will instead drop the extra read and open the press-both case as its own issue with those measurements.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7250532587 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1949#issuecomment-7250532587). Originally posted by [SXM081115](https://github.com/SXM081115) on 2026-11-29T04:14:22Z. --- You are right on the queue, and I have replaced the approach in `64e5b7d` rather than argue for it. **What was wrong.** The poll runs on the worker `Hub::start` spawns (`gameinput.rs:519-524`, 8 ms), and `mnk::sample()` is reached from there (`gameinput.rs:217`) — not on the thread that pumps the window's messages. `GetKeyState` there reflects a queue nothing advances, so the extra read was effectively empty and the OR degenerated into the previous read. Thank you for catching it before it went in as "fixed". **What the branch does now.** - The **title window's own procedure** — the hook in `lifecycle.rs` that already runs `capture_mouse`/`english_input`, i.e. the thread that pumps — records every `WM_LBUTTONDOWN/UP`, `WM_RBUTTONDOWN/UP`, `WM_MBUTTONDOWN/UP` and `WM_XBUTTONDOWN/UP` (high word picks Mouse4/Mouse5) into an `AtomicU32`, one bit per virtual-key code. - `mnk::sample()` reads `GetAsyncKeyState(vk)` **or** that bit, for **mouse VKs only** (`1..=6`). Keyboard bindings in `b.axes` and `b.buttons` keep exactly the single read they had, so the previous keyboard behaviour is preserved, as you suggested. - `WM_KILLFOCUS` / `WM_NCDESTROY` clears the mask: when the focus is gone there is no up message to come. - The message is let through to the title's own procedure unchanged (`CallWindowProcW` as before); nothing is swallowed. **On release timing.** The concern was that OR-ing two sources could delay a release. Here the release is not a queue's read progress but the actual `WM_*BUTTONUP`, which the pump always receives for a captured window; `GetAsyncKeyState` only adds an "up" earlier, never later, because a bit is cleared on the real up and on focus loss. So a held-then-released button cannot stay stuck. The one case worth watching is a button released while the window is not captured and never gets the message — cleared by the focus-loss path above. **Tests I will add** (in this branch, native, Windows): press both, then release them **one at a time in both orders**, asserting the reported trigger pair after each step, and the frame at which each release shows up. Plus the existing hold-both test. If you would rather keep the change mouse-only *and* without the pump mask, say so and I will instead drop the extra read and open the press-both case as its own issue with those measurements.
ANDREANIADS commented 2026-11-29 04:35:38 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by ANDREANIADS on 2026-11-29T04:35:38Z.


One case for the planned Windows test at 64e5b7d: in windowed mode with VICTORY_MOUSE_CAPTURE off, hold Mouse1 inside the window, open F1, move outside the client area without changing focus, release there, then close F1 by keyboard. Check which window owns mouse capture, whether the title received WM_LBUTTONUP, and the first RT sample after closing. Repeat for Mouse2/LT.

F1 returns a neutral pad before recentering, but resetting Mouse leaves the separate button mask intact. In a CPU replay with async-up, local release and focus loss each cleared the bit for both buttons. Omitting the local release and focus loss left the bit set after closing F1. Native message delivery remains to be checked: the optional ClipCursor confines the pointer, while mouse capture determines where an outside release is delivered. Microsoft: WM_LBUTTONUP

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7250745890 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1949#issuecomment-7250745890). Originally posted by [ANDREANIADS](https://github.com/ANDREANIADS) on 2026-11-29T04:35:38Z. --- One case for the planned Windows test at `64e5b7d`: in windowed mode with `VICTORY_MOUSE_CAPTURE` off, hold Mouse1 inside the window, open F1, move outside the client area without changing focus, release there, then close F1 by keyboard. Check which window owns mouse capture, whether the title received `WM_LBUTTONUP`, and the first RT sample after closing. Repeat for Mouse2/LT. F1 returns a neutral pad before recentering, but resetting `Mouse` leaves the separate button mask intact. In a CPU replay with async-up, local release and focus loss each cleared the bit for both buttons. Omitting the local release and focus loss left the bit set after closing F1. Native message delivery remains to be checked: the optional `ClipCursor` confines the pointer, while mouse capture determines where an outside release is delivered. [Microsoft: WM_LBUTTONUP](https://learn.microsoft.com/en-us/windows/win32/inputdev/wm-lbuttonup)
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
trustno1-coder/VIctory-NVIDIA!1949
No description provided.