GameInput: held keys come back in virtual-key order, not in the order they were pressed #874

Open
opened 2026-11-26 04:14:06 +00:00 by github-bot · 2 comments
Owner

@⁠BastienRodz on GitHub · 2026-11-24 13:04 UTC · original

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

Fix

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.

The fix is ready on a branch of my fork, since PR creation is limited to collaborators: https://github.com/BastienRodz/VIctory/commits/hle/held-keys-in-press-order (one commit on main ffe310e, gameinput.rs +37/-1).

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.

**@⁠BastienRodz** on GitHub · 2026-11-24 13:04 UTC · [original](https://github.com/Marvisak/VIctory/issues/874) ## 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`). ## Fix 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. The fix is ready on a branch of my fork, since PR creation is limited to collaborators: https://github.com/BastienRodz/VIctory/commits/hle/held-keys-in-press-order (one commit on `main` `ffe310e`, `gameinput.rs` +37/-1). ## 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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:14:06 +00:00
Author
Owner

@⁠MichaelSmithAI on GitHub · 2026-11-24 13:20 UTC · original

The guest GetKeyState check confirms the fix on Win64 under Wine. With Z held on one poll and A added on the next, ffe310e returns [A, Z] and 4ce99c1 returns [Z, A]. Release/repress, a two-key caller buffer, and reading older retained snapshots after later polls all passed. The keyboard states were injected at the host query boundary. The test is in BastienRodz/VIctory#2.

For anyone combining this with #656, both branches edit Host::keys. Building the character state from the current held set and emitting the reading in remembered press order preserves both behaviors. All 11 GameInput tests passed with the new fixture on that merge, including the character and modifier tests. Tested merge resolution

**@⁠MichaelSmithAI** on GitHub · 2026-11-24 13:20 UTC · [original](https://github.com/Marvisak/VIctory/issues/874#issuecomment-7132690014) The guest GetKeyState check confirms the fix on Win64 under Wine. With Z held on one poll and A added on the next, ffe310e returns [A, Z] and 4ce99c1 returns [Z, A]. Release/repress, a two-key caller buffer, and reading older retained snapshots after later polls all passed. The keyboard states were injected at the host query boundary. The test is in [BastienRodz/VIctory#2](https://github.com/BastienRodz/VIctory/pull/2). For anyone combining this with #656, both branches edit Host::keys. Building the character state from the current held set and emitting the reading in remembered press order preserves both behaviors. All 11 GameInput tests passed with the new fixture on that merge, including the character and modifier tests. [Tested merge resolution](https://github.com/MichaelSmithAI/VIctory/commit/f55e148f8d587d134598df7d6f64bcc2733fa1d4)
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 06:55 UTC · original

Automated triage for the maintainers.

Confirmed on main. In crates/victory-hle/src/modules/gameinput.rs, host::Host::keys rebuilds the held keys on each poll by scanning 0x08..=0xFE in ascending order and keeps no earlier order, so key_state returns them in virtual-key order and not in the press order GameInput documents. Your fix is open as PR #927 (hle/held-keys-in-press-order), so review continues there. The comment above has a tested merge with #656, which edits the same function.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 06:55 UTC · [original](https://github.com/Marvisak/VIctory/issues/874#issuecomment-7155307699) <!-- victory-issue-triage v1 verdict=confirmed seen=2026-11-24T13:20:28Z at=2026-11-25T06:55:53Z --> Automated triage for the maintainers. Confirmed on `main`. In `crates/victory-hle/src/modules/gameinput.rs`, `host::Host::keys` rebuilds the held keys on each poll by scanning `0x08..=0xFE` in ascending order and keeps no earlier order, so `key_state` returns them in virtual-key order and not in the press order GameInput documents. Your fix is open as PR #927 (`hle/held-keys-in-press-order`), so review continues there. The comment above has a tested merge with #656, which edits the same function. --- _Generated by [Claude Code](https://claude.ai/code)_
This discussion has been locked. Commenting is limited to contributors.
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#874
No description provided.