From 4ce99c16241356c919181bc9d9101fd69920a0ca Mon Sep 17 00:00:00 2001 From: BastienRdz <58186763+BastienRodz@users.noreply.github.com> Date: Tue, 24 Nov 2026 12:59:50 +0000 Subject: [PATCH 1/3] GameInput: held keys are reported in the order they were pressed, as documented 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 Co-Authored-By: Claude Opus 5.5 --- crates/victory-hle/src/modules/gameinput.rs | 38 ++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/crates/victory-hle/src/modules/gameinput.rs b/crates/victory-hle/src/modules/gameinput.rs index a411bd0f4a..41792f3c60 100644 --- a/crates/victory-hle/src/modules/gameinput.rs +++ b/crates/victory-hle/src/modules/gameinput.rs @@ -1067,6 +1067,18 @@ pub(crate) fn module() -> HleModule { } } +/// Keep held keys in press order: "New keys are added at the end of the array, +/// and when a key is released, everything shifts down to fill in the gap." +#[cfg(any(windows, test))] +fn press_order(previous: &[u8], down: &[u8]) -> Vec { + previous + .iter() + .filter(|vk| down.contains(vk)) + .chain(down.iter().filter(|vk| !previous.contains(vk))) + .copied() + .collect() +} + /// Host input: XInput pads, the keyboard while our process has focus. #[cfg(windows)] mod host { @@ -1130,6 +1142,7 @@ mod host { #[derive(Default)] pub(super) struct Host { empty_until: [Option; PADS], + held_keys: Vec, } impl Source for Host { @@ -1162,7 +1175,10 @@ mod host { .collect(); down.extend(held); } - down.into_iter() + self.held_keys = press_order(&self.held_keys, &down); + self.held_keys + .iter() + .copied() .map(|vk| KeyState { // SAFETY: plain query. scan_code: unsafe { MapVirtualKeyW(u32::from(vk), MAPVK_VK_TO_VSC_EX) }, @@ -1612,4 +1628,24 @@ mod tests { rumble(device, std::ptr::null()); assert_eq!(host.lock().unwrap().rumble, [(2, 32767, 65535), (2, 0, 0)]); } + + #[test] + fn held_keys_keep_their_press_order() { + let held = press_order(&[], b"Z"); + assert_eq!(held, b"Z"); + + // A has a lower virtual-key code than Z, but Z was already held. + let held = press_order(&held, b"AZ"); + assert_eq!(held, b"ZA"); + + // Adding B appends at the end while keeping existing order. + let with_b = press_order(&held, b"ABZ"); + assert_eq!(with_b, b"ZAB"); + + // Releasing Z shifts remaining keys down; re-pressing Z puts it at the end. + let held = press_order(&held, b"A"); + assert_eq!(held, b"A"); + let held = press_order(&held, b"AZ"); + assert_eq!(held, b"AZ"); + } } From 185b6decf5ab387ebb083ff0164dd13a7382638a Mon Sep 17 00:00:00 2001 From: swarm Date: Tue, 24 Nov 2026 08:20:08 -0500 Subject: [PATCH 2/3] GameInput tests: check key order through retained readings --- crates/victory-hle/src/modules/gameinput.rs | 65 ++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/crates/victory-hle/src/modules/gameinput.rs b/crates/victory-hle/src/modules/gameinput.rs index 41792f3c60..38f246faef 100644 --- a/crates/victory-hle/src/modules/gameinput.rs +++ b/crates/victory-hle/src/modules/gameinput.rs @@ -1105,6 +1105,27 @@ mod host { /// Asking XInput about an empty slot is slow; look again this often. const EMPTY_SLOT_RECHECK: Duration = Duration::from_secs(1); + #[cfg(test)] + std::thread_local! { + static TEST_KEYS: std::cell::Cell> = const { + std::cell::Cell::new(None) + }; + } + + #[cfg(test)] + pub(super) fn set_test_keys(keys: Option<&'static [u8]>) { + TEST_KEYS.set(keys); + } + + fn key_is_down(vk: u8) -> bool { + #[cfg(test)] + if let Some(keys) = TEST_KEYS.get() { + return keys.contains(&vk); + } + // SAFETY: plain query. + (unsafe { GetAsyncKeyState(i32::from(vk)) }) < 0 + } + const BUTTONS: [(u16, u32); 14] = [ (XINPUT_GAMEPAD_START, MENU), (XINPUT_GAMEPAD_BACK, VIEW), @@ -1171,7 +1192,7 @@ mod host { let held: Vec = (0x08..=0xFE_u8) .filter(|vk| !(0x10..=0x12).contains(vk) && !down.contains(vk)) // SAFETY: plain queries. - .filter(|&vk| unsafe { GetAsyncKeyState(i32::from(vk)) } < 0) + .filter(|&vk| key_is_down(vk)) .collect(); down.extend(held); } @@ -1241,6 +1262,10 @@ mod host { /// The foreground window is one of ours (the title's or our fallback). fn focused() -> bool { + #[cfg(test)] + if TEST_KEYS.get().is_some() { + return true; + } let mut process = 0; // SAFETY: plain queries; a null window gives process 0. unsafe { @@ -1525,6 +1550,44 @@ mod tests { assert_eq!(current(hub, GAMEPAD_KIND, KEYBOARD), Err(READING_NOT_FOUND)); } + #[cfg(windows)] + #[test] + fn key_readings_preserve_press_order_and_existing_snapshots() { + let hub = Hub::new(Box::new(host::Host::default())); + let poll = |keys: &'static [u8]| { + host::set_test_keys(Some(keys)); + hub.poll(); + host::set_test_keys(None); + current(hub, KEYBOARD_KIND, KEYBOARD).unwrap() + }; + let keys = |reading, capacity| { + let state = slot:: u32, _>( + reading, 0x78, + ); + let mut output = [KeyState::default(); 3]; + let count = state(reading, capacity, output.as_mut_ptr()); + output[..count as usize] + .iter() + .map(|key| key.virtual_key) + .collect::>() + }; + let z = poll(b"Z"); + let za = poll(b"AZ"); + let zab = poll(b"ABZ"); + let ab = poll(b"AB"); + let abz = poll(b"ABZ"); + + assert_eq!(keys(z, 3), b"Z"); + assert_eq!(keys(za, 3), b"ZA"); + assert_eq!(keys(zab, 3), b"ZAB"); + assert_eq!(keys(zab, 2), b"ZA"); + assert_eq!(keys(ab, 3), b"AB"); + assert_eq!(keys(abz, 3), b"ABZ"); + for reading in [z, za, zab, ab, abz] { + release(reading); + } + } + #[test] fn async_enumeration_waits_for_the_poll_and_unregistering_stops_callbacks() { let (hub, host) = fake(); From d8f922116030479b5a46014b9cf286b7c578ee43 Mon Sep 17 00:00:00 2001 From: BastienRdz <58186763+BastienRodz@users.noreply.github.com> Date: Tue, 24 Nov 2026 12:59:50 +0000 Subject: [PATCH 3/3] GameInput: held keys are reported in the order they were pressed, as documented 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 Co-Authored-By: Claude Opus 5.5 --- crates/victory-hle/src/modules/gameinput.rs | 36 ++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/crates/victory-hle/src/modules/gameinput.rs b/crates/victory-hle/src/modules/gameinput.rs index 2e7115d6fc..98ab58c991 100644 --- a/crates/victory-hle/src/modules/gameinput.rs +++ b/crates/victory-hle/src/modules/gameinput.rs @@ -1115,6 +1115,18 @@ pub(crate) fn module() -> HleModule { } } +/// Keep held keys in press order: "New keys are added at the end of the array, +/// and when a key is released, everything shifts down to fill in the gap." +#[cfg(any(windows, test))] +fn press_order(previous: &[u8], down: &[u8]) -> Vec { + previous + .iter() + .filter(|vk| down.contains(vk)) + .chain(down.iter().filter(|vk| !previous.contains(vk))) + .copied() + .collect() +} + /// Host input: XInput pads, the keyboard while our process has focus. #[cfg(windows)] mod host { @@ -1178,6 +1190,7 @@ mod host { #[derive(Default)] pub(super) struct Host { empty_until: [Option; PADS], + held_keys: Vec, } fn key_state(vk: u8) -> KeyState { @@ -1221,7 +1234,8 @@ mod host { .collect(); down.extend(held); } - down.into_iter().map(key_state).collect() + self.held_keys = press_order(&self.held_keys, &down); + self.held_keys.iter().copied().map(key_state).collect() } #[cfg(any(test, feature = "diagnostics"))] @@ -1762,4 +1776,24 @@ mod tests { assert_eq!(pad(r), physical); release(r); } + + #[test] + fn held_keys_keep_their_press_order() { + let held = press_order(&[], b"Z"); + assert_eq!(held, b"Z"); + + // A has a lower virtual-key code than Z, but Z was already held. + let held = press_order(&held, b"AZ"); + assert_eq!(held, b"ZA"); + + // Adding B appends at the end while keeping existing order. + let with_b = press_order(&held, b"ABZ"); + assert_eq!(with_b, b"ZAB"); + + // Releasing Z shifts remaining keys down; re-pressing Z puts it at the end. + let held = press_order(&held, b"A"); + assert_eq!(held, b"A"); + let held = press_order(&held, b"AZ"); + assert_eq!(held, b"AZ"); + } }