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

Open
github-bot wants to merge 2 commits from github-bot/gh-927 into main AGit
Owner

@⁠BastienRodz on GitHub · 2026-11-24 14:17 UTC · original
BastienRodz:hle/held-keys-in-press-order → main · +47 −1 · 1 files · 2 commits · open

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

**@⁠BastienRodz** on GitHub · 2026-11-24 14:17 UTC · [original](https://github.com/Marvisak/VIctory/pull/927) `BastienRodz:hle/held-keys-in-press-order` → `main` · +47 −1 · 1 files · 2 commits · open ## 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)
Microsoft's GameInput documentation for IGameInputReading::GetKeyState
states: "New keys are added at the end of the array, and when a key is
released, everything shifts down to fill in the gap." Previously,
host::Host::keys rebuilt the held-key list on every poll in ascending
virtual-key order, so holding Z and then pressing A reported [A, Z]
instead of [Z, A].

Remember the previous poll's held virtual keys on host::Host and order
each poll's result with a pure press_order helper that keeps still-held
keys in their prior order and appends newly pressed keys at the end.

Co-Authored-By: Gemini 4 Neon <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
From review: `press_order` appended a key as often as a reading listed
it (scripted VICTORY_KEYS in diagnostics builds can). It now appends each
key once. The test also releases a key in the middle of the held ones.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:14:18 +00:00
Author
Owner

@⁠Zipettte on GitHub · 2026-11-24 15:40 UTC · original

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).

**@⁠Zipettte** on GitHub · 2026-11-24 15:40 UTC · [original](https://github.com/Marvisak/VIctory/pull/927#issuecomment-7136435883) 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)._
Author
Owner

@⁠BastienRodz on GitHub · 2026-11-25 00:27 UTC · original

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.

**@⁠BastienRodz** on GitHub · 2026-11-25 00:27 UTC · [original](https://github.com/Marvisak/VIctory/pull/927#issuecomment-7148532634) 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.
Author
Owner

@⁠BastienRodz on GitHub · 2026-11-25 00:31 UTC · original

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** on GitHub · 2026-11-25 00:31 UTC · [original](https://github.com/Marvisak/VIctory/pull/927#issuecomment-7148602177) 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.
Author
Owner

@⁠BastienRodz on GitHub · 2026-11-25 00:56 UTC · original

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.

**@⁠BastienRodz** on GitHub · 2026-11-25 00:56 UTC · [original](https://github.com/Marvisak/VIctory/pull/927#issuecomment-7149050128) 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.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 02:32 UTC · original · edited 2026-11-26 03:18 UTC

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

**@⁠Marvisak** on GitHub · 2026-11-25 02:32 UTC · [original](https://github.com/Marvisak/VIctory/pull/927#issuecomment-7150865065) · edited 2026-11-26 03:18 UTC <!-- 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)_
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 01:19 UTC · original

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.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 01:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/927#issuecomment-7179662398) 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.
Author
Owner

@⁠BastienRodz on GitHub · 2026-11-26 01:24 UTC · original

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** on GitHub · 2026-11-26 01:24 UTC · [original](https://github.com/Marvisak/VIctory/pull/927#issuecomment-7179743049) 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.
Author
Owner

@⁠BastienRodz on GitHub · 2026-11-26 02:19 UTC · original

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.

**@⁠BastienRodz** on GitHub · 2026-11-26 02:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/927#issuecomment-7180694984) 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 can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/927/head:github-bot/gh-927'
git switch 'github-bot/gh-927'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-927'
git switch 'github-bot/gh-927'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-927'
git switch 'github-bot/gh-927'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-927'
git switch 'main'
git merge --squash 'github-bot/gh-927'
git switch 'main'
git merge --ff-only 'github-bot/gh-927'
git switch 'main'
git merge 'github-bot/gh-927'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!927
No description provided.