GameInput: held keys are reported in the order they were pressed, as documented #927

Open
BastienRodz wants to merge 2 commits from upstream-pr/927 into main
BastienRodz commented 2026-11-24 14:17:20 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by BastienRodz on 2026-11-24T14:17:20Z.


What's wrong

Microsoft's GameInput keyboard and mouse documentation specifies the order of the array returned by IGameInputReading::GetKeyState:

"New keys are added at the end of the array, and when a key is released, everything shifts down to fill in the gap."

In crates/victory-hle/src/modules/gameinput.rs:1158-1163, host::Host::keys rebuilds the held-key list on every poll by scanning virtual-key codes in ascending numerical order:

let held: Vec<u8> = (0x08..=0xFE_u8)
    .filter(|vk| !(0x10..=0x12).contains(vk) && !down.contains(vk))
    // SAFETY: plain queries.
    .filter(|&vk| unsafe { GetAsyncKeyState(i32::from(vk)) } < 0)
    .collect();
down.extend(held);

key_state (crates/victory-hle/src/modules/gameinput.rs:1003-1014) copies that list directly into the caller's GameInputKeyState buffer. If the player holds Z (0x5A) and then also presses A (0x41), GameInput documents [Z, A], while VIctory reports [A, Z].

Impact

Key-binding screens and any title code that identifies the most recently pressed key from the end of the GetKeyState array read the wrong key whenever a newly pressed key has a lower virtual-key code than a key already held (for example, binding Z when the player pressed A).

What the fix does

Track the previous poll's held virtual keys on host::Host and compute the next ordered list with a small pure press_order(previous, down) helper: keep keys from previous that are still in down in their existing order, then append keys in down that were not in previous. Because PR #656 also edits Host::keys, the helper lives outside keys and the change inside keys is limited to applying press_order and iterating self.held_keys, leaving key scanning, filtering, and KeyState construction untouched.

Tests

held_keys_keep_their_press_order exercises press_order with these sequences:

  • Z held, then A pressed: [Z, A]. Today's scan gives [A, Z].
  • B added: [Z, A, B].
  • Z released: [A].
  • Z pressed again: it goes to the end, [A, Z].

On GitHub's windows-latest, with the upstream CI's commands (clippy -D warnings with and without --all-features, then cargo test --workspace --locked), everything passes: victory-hle 95 passed, 0 failed (https://github.com/BastienRodz/VIctory/actions/runs/45399104790).

Not tested: a title's key-binding screen.

No game data used. AI-assisted: found with GPT-6.2 Astra, written with Gemini 4 Neon, reviewed and verified with Claude Code.

Fixes #874.

🤖 Generated with Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:issue:6583826058 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/927). Originally posted by [BastienRodz](https://github.com/BastienRodz) on 2026-11-24T14:17:20Z. --- ## What's wrong Microsoft's [GameInput keyboard and mouse documentation](https://learn.microsoft.com/en-us/gaming/gdk/docs/features/common/input/advanced/input-keyboard-mouse#reading-key-state) specifies the order of the array returned by `IGameInputReading::GetKeyState`: > "New keys are added at the end of the array, and when a key is released, everything shifts down to fill in the gap." In `crates/victory-hle/src/modules/gameinput.rs:1158-1163`, `host::Host::keys` rebuilds the held-key list on every poll by scanning virtual-key codes in ascending numerical order: ```rust let held: Vec<u8> = (0x08..=0xFE_u8) .filter(|vk| !(0x10..=0x12).contains(vk) && !down.contains(vk)) // SAFETY: plain queries. .filter(|&vk| unsafe { GetAsyncKeyState(i32::from(vk)) } < 0) .collect(); down.extend(held); ``` `key_state` (`crates/victory-hle/src/modules/gameinput.rs:1003-1014`) copies that list directly into the caller's `GameInputKeyState` buffer. If the player holds `Z` (`0x5A`) and then also presses `A` (`0x41`), GameInput documents `[Z, A]`, while VIctory reports `[A, Z]`. ## Impact Key-binding screens and any title code that identifies the most recently pressed key from the end of the `GetKeyState` array read the wrong key whenever a newly pressed key has a lower virtual-key code than a key already held (for example, binding `Z` when the player pressed `A`). ## What the fix does Track the previous poll's held virtual keys on `host::Host` and compute the next ordered list with a small pure `press_order(previous, down)` helper: keep keys from `previous` that are still in `down` in their existing order, then append keys in `down` that were not in `previous`. Because PR #656 also edits `Host::keys`, the helper lives outside `keys` and the change inside `keys` is limited to applying `press_order` and iterating `self.held_keys`, leaving key scanning, filtering, and `KeyState` construction untouched. ## Tests `held_keys_keep_their_press_order` exercises `press_order` with these sequences: - Z held, then A pressed: `[Z, A]`. Today's scan gives `[A, Z]`. - B added: `[Z, A, B]`. - Z released: `[A]`. - Z pressed again: it goes to the end, `[A, Z]`. On GitHub's `windows-latest`, with the upstream CI's commands (clippy `-D warnings` with and without `--all-features`, then `cargo test --workspace --locked`), everything passes: `victory-hle` 95 passed, 0 failed (https://github.com/BastienRodz/VIctory/actions/runs/45399104790). **Not tested:** a title's key-binding screen. No game data used. AI-assisted: found with GPT-6.2 Astra, written with Gemini 4 Neon, reviewed and verified with Claude Code. Fixes #874. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
DenRakEiw commented 2026-11-24 15:03:22 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-24T14:21:17Z.

Review: APPROVED


Tested head 4ce99c1 (fast-forward on main ffe310e) on Windows 11 x64 (Rust 1.98.1 MSVC):

  • cargo test -p victory-hle: 95 passed, 0 failed (plus no_dll_planting: 1 passed), including modules::gameinput::tests::held_keys_keep_their_press_order
  • cargo clippy -p victory-hle --all-targets: 0 warnings
  • cargo xtask policy: ok

Clean fix preserving press order for held keys. Approving.

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6040908314 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/927#pullrequestreview-6040908314). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-24T14:21:17Z. **Review:** APPROVED --- Tested head `4ce99c1` (fast-forward on `main` `ffe310e`) on Windows 11 x64 (Rust 1.98.1 MSVC): - `cargo test -p victory-hle`: **95 passed, 0 failed** (plus `no_dll_planting`: 1 passed), including `modules::gameinput::tests::held_keys_keep_their_press_order` - `cargo clippy -p victory-hle --all-targets`: **0 warnings** - `cargo xtask policy`: ok Clean fix preserving press order for held keys. Approving. AI-assisted (Gemini 4 Neon).
Zipettte commented 2026-11-24 16:05:19 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Zipettte on 2026-11-24T15:40:59Z.


Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at 4ce99c1: cargo test -p victory-hle passes, 96 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).

Tested with AI assistance (Claude).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7136435883 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/927#issuecomment-7136435883). Originally posted by [Zipettte](https://github.com/Zipettte) on 2026-11-24T15:40:59Z. --- Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at 4ce99c1: `cargo test -p victory-hle` passes, 96 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed). _Tested with AI assistance (Claude)._
BastienRodz commented 2026-11-25 01:07:50 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by BastienRodz on 2026-11-25T00:27:39Z.


Rebased onto current main. keys() now builds its KeyStates through key_state, so the change is just press_order applied before that map. gameinput tests: 10 passed under Wine, held_keys_keep_their_press_order and scripted_keys_merge_into_connected_host_pad included.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148532634 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/927#issuecomment-7148532634). Originally posted by [BastienRodz](https://github.com/BastienRodz) on 2026-11-25T00:27:39Z. --- Rebased onto current `main`. `keys()` now builds its `KeyState`s through `key_state`, so the change is just `press_order` applied before that map. `gameinput` tests: 10 passed under Wine, `held_keys_keep_their_press_order` and `scripted_keys_merge_into_connected_host_pad` included.
BastienRodz commented 2026-11-25 01:07:59 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by BastienRodz on 2026-11-25T00:31:51Z.


The CI failure is only victory-shader's gathers_at_a_level_match_the_hardware and permutes_read_the_lane_named_and_zero_from_lanes_left_out. main fails those two the same way on the runner's software adapter, and #1752 fixes them. victory-hle, the only crate this PR touches, passes, gameinput tests included.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148602177 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/927#issuecomment-7148602177). Originally posted by [BastienRodz](https://github.com/BastienRodz) on 2026-11-25T00:31:51Z. --- The CI failure is only `victory-shader`'s `gathers_at_a_level_match_the_hardware` and `permutes_read_the_lane_named_and_zero_from_lanes_left_out`. `main` fails those two the same way on the runner's software adapter, and #1752 fixes them. `victory-hle`, the only crate this PR touches, passes, `gameinput` tests included.
BastienRodz commented 2026-11-25 01:08:19 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by BastienRodz on 2026-11-25T00:56:45Z.


Still red on the same two victory-shader tests: main's own CI after 42363ab fails them too. The runner's Basic Render Driver doesn't set the software flag that commit checks. #1919 adds its vendor/device ids, and windows-latest is green with it. This PR's crate passes.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7149050128 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/927#issuecomment-7149050128). Originally posted by [BastienRodz](https://github.com/BastienRodz) on 2026-11-25T00:56:45Z. --- Still red on the same two `victory-shader` tests: `main`'s own CI after `42363ab` fails them too. The runner's Basic Render Driver doesn't set the software flag that commit checks. #1919 adds its vendor/device ids, and `windows-latest` is green with it. This PR's crate passes.
Marvisak commented 2026-11-25 03:03:33 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T02:32:59Z.


Automated triage for the maintainers.

Verdict: merge candidate. Re-reviewed at 0832e0a, which merges cleanly into main. ISSUE_DRAFT.md is gone, and the PR touches only crates/victory-hle/src/modules/gameinput.rs, as you said. press_order keeps the previous poll's held keys in order and appends new ones once. host::Host::keys now reports held_keys in that order, which matches the documented GetKeyState ordering. held_keys_keep_their_press_order covers a lower virtual-key code pressed second, releasing a middle key and the first key, pressing a key again, and a duplicate in one reading.

CI: passing. Recommending a look, not approving.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7150865065 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/927#issuecomment-7150865065). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T02:32:59Z. --- <!-- victory-triage v1 head=0832e0a582415064a7f946bebdec8d297fa4fdac verdict=merge-candidate at=2026-11-26T03:18:57Z --> Automated triage for the maintainers. **Verdict: merge candidate.** Re-reviewed at `0832e0a`, which merges cleanly into `main`. `ISSUE_DRAFT.md` is gone, and the PR touches only `crates/victory-hle/src/modules/gameinput.rs`, as you said. `press_order` keeps the previous poll's held keys in order and appends new ones once. `host::Host::keys` now reports `held_keys` in that order, which matches the documented GetKeyState ordering. `held_keys_keep_their_press_order` covers a lower virtual-key code pressed second, releasing a middle key and the first key, pressing a key again, and a duplicate in one reading. CI: passing. Recommending a look, not approving. --- _Generated by [Claude Code](https://claude.ai/code)_
fuzeofficlal commented 2026-11-26 02:06:58 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T01:19:39Z.


Code review and Windows 11 verification at head 10dd2549d4da merged cleanly onto main (63bcca43c6a0):

  • Code review (crates/victory-hle/src/modules/gameinput.rs:1145-1152, 1217, 1261-1262):
    • press_order retains held keys in prior press order and appends newly pressed keys, clears cleanly on focus loss (press_order(&self.held_keys, &[])), and integrates cleanly with State::update ordered equality and key_state truncation.
    • Two minor non-blocking notes: (1) under diagnostics, duplicate VKs emitted in the same VICTORY_KEYS window aren't deduped before appending; (2) in held_keys_keep_their_press_order (line 1916), passing &held (b"ZA") instead of &with_b (b"ZAB") only tests releasing the first key rather than releasing a middle key.
  • Verified (Windows 11, Rust 1.98.1):
    • cargo xtask policy: 191 files ok; cargo clippy --locked -p victory-hle --tests -- -D warnings: 0 warnings
    • victory-hle -- gameinput: 11/11 passed.

LGTM.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7179662398 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/927#issuecomment-7179662398). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T01:19:39Z. --- Code review and Windows 11 verification at head `10dd2549d4da` merged cleanly onto `main` (`63bcca43c6a0`): - **Code review (`crates/victory-hle/src/modules/gameinput.rs:1145-1152, 1217, 1261-1262`)**: - `press_order` retains held keys in prior press order and appends newly pressed keys, clears cleanly on focus loss (`press_order(&self.held_keys, &[])`), and integrates cleanly with `State::update` ordered equality and `key_state` truncation. - Two minor non-blocking notes: (1) under `diagnostics`, duplicate VKs emitted in the same `VICTORY_KEYS` window aren't deduped before appending; (2) in `held_keys_keep_their_press_order` (line 1916), passing `&held` (`b"ZA"`) instead of `&with_b` (`b"ZAB"`) only tests releasing the first key rather than releasing a middle key. - **Verified (Windows 11, Rust 1.98.1)**: - `cargo xtask policy`: `191 files ok`; `cargo clippy --locked -p victory-hle --tests -- -D warnings`: 0 warnings - `victory-hle -- gameinput`: **11/11 passed**. LGTM.
BastienRodz commented 2026-11-26 02:07:05 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by BastienRodz on 2026-11-26T01:24:32Z.


Thanks, both notes taken in 6166c20:

  1. press_order now appends each key once, so a key listed twice in one reading (scripted VICTORY_KEYS in diagnostics builds) is held once.
  2. The test now releases a middle key (ZAB with BZ down gives ZB) and the first key (ZAB with AB down gives AB), re-presses Z at the end, and checks AA gives A.

gameinput tests (11) pass under Wine, and cargo clippy --workspace --all-targets --locked -- -D warnings is clean.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7179743049 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/927#issuecomment-7179743049). Originally posted by [BastienRodz](https://github.com/BastienRodz) on 2026-11-26T01:24:32Z. --- Thanks, both notes taken in `6166c20`: 1. `press_order` now appends each key once, so a key listed twice in one reading (scripted `VICTORY_KEYS` in diagnostics builds) is held once. 2. The test now releases a middle key (`ZAB` with `BZ` down gives `ZB`) and the first key (`ZAB` with `AB` down gives `AB`), re-presses Z at the end, and checks `AA` gives `A`. `gameinput` tests (11) pass under Wine, and `cargo clippy --workspace --all-targets --locked -- -D warnings` is clean.
BastienRodz commented 2026-11-26 03:05:07 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by BastienRodz on 2026-11-26T02:19:38Z.


Removed: ISSUE_DRAFT.md was a stray untracked file in my checkout, and my commit picked it up by mistake. 0832e0a is the same change without it; the PR touches crates/victory-hle/src/modules/gameinput.rs only.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7180694984 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/927#issuecomment-7180694984). Originally posted by [BastienRodz](https://github.com/BastienRodz) on 2026-11-26T02:19:38Z. --- Removed: `ISSUE_DRAFT.md` was a stray untracked file in my checkout, and my commit picked it up by mistake. `0832e0a` is the same change without it; the PR touches `crates/victory-hle/src/modules/gameinput.rs` only.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/927:upstream-pr/927'
git switch 'upstream-pr/927'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/927'
git switch 'upstream-pr/927'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/927'
git switch 'upstream-pr/927'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/927'
git switch 'main'
git merge --squash 'upstream-pr/927'
git switch 'main'
git merge --ff-only 'upstream-pr/927'
git switch 'main'
git merge 'upstream-pr/927'
git push origin 'main'
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
Marvisak/VIctory!927
No description provided.