From 3ea50a7ae3e5239f8b3c0f7f84ece8c43d592c66 Mon Sep 17 00:00:00 2001 From: Guoliang Chen Date: Wed, 16 Sep 2026 03:05:06 -0700 Subject: [PATCH] input-inject: chord and lock-hotkey match on the raw VK (QA finding 1) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `injectKey` normalised the incoming virtual key before handing it to `KeyState.process`, so a side-less `VK_SHIFT`/`VK_CONTROL`/`VK_MENU` set `chord.shift`/`.control`/`.option`. Windows' `InputProcessKeyEx` matches side-specific VKs only — `VK.LSHIFT`/`VK.RSHIFT`, `VK.LCONTROL`/ `VK.RCONTROL`, `VK.LMENU`/`VK.RMENU`, `VK.LWIN`/`VK.RWIN` in both its switches (`InputSimulation.cs:372-403` up, `:418-438` down) — and `Common.HotkeyMatched` (`:406`) is given the raw `vkCode`, so those side-less codes, which the lock-chord replay puts on the wire (`InputHook.cs:485-512`), never move the chord booleans (docs/protocol.md §10.5 note 3). The chord state and the hotkey test now see `data.virtualKey` unchanged; `KeyState.normalise(vk:)` is applied only on the posting path, where "macOS has no keycode for either Shift" actually applies. Without this, a replayed lock chord locked a Mac peer for any `HotKeyLockMachine` requiring Ctrl or Alt while the Windows peer beside it ignored the same bytes — wire compatibility, invariant 1. Co-Authored-By: Claude Opus 5 (1M context) --- Sources/MWBMac/Injector.swift | 17 +++++++++++++++-- Sources/MWBMac/KeyState.swift | 16 ++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/Sources/MWBMac/Injector.swift b/Sources/MWBMac/Injector.swift index 2def12a..665415f 100644 --- a/Sources/MWBMac/Injector.swift +++ b/Sources/MWBMac/Injector.swift @@ -120,9 +120,19 @@ public final class Injector: @unchecked Sendable { // MARK: - Keyboard (`SendKey`, `InputSimulation.cs:108-157`) private func injectKey(_ data: KeyboardData) { - let vk = KeyState.normalise(vk: data.virtualKey) let isUp = (data.flags & KeyFlag.up) != 0 - let decision = state.process(vk: vk, isUp: isUp, lockMachine: config.lockMachine) + // The chord state and the lock-hotkey test see the **raw** virtual key, + // never a normalised one: `InputProcessKeyEx`'s two switches + // (`InputSimulation.cs:372-403` up, `:418-438` down) have only + // side-specific `case` labels, so a side-less `VK_SHIFT`/`VK_CONTROL`/ + // `VK_MENU` — which `ReleaseAllKeys` (`InitAndCleanup.cs:225-229`) and + // the lock-chord replay (`InputHook.cs:485-512`) both put on the wire — + // falls through `default` and leaves `shiftDown`/`ctrlDown`/`altDown` + // alone (docs/protocol.md §10.5 note 3). `HotkeyMatched` (`:406`) is + // likewise given the raw `vkCode`. Normalising first would make this Mac + // lock on a replayed chord that the Windows peer beside it ignores. + let decision = state.process( + vk: data.virtualKey, isUp: isUp, lockMachine: config.lockMachine) if decision.releaseAllKeys { releaseAllOnQueue() } if decision.lockMachine { runLock() } @@ -132,6 +142,9 @@ public final class Injector: @unchecked Sendable { log.debug("remote chord intercepted; key swallowed") return } + // The posting path, and only it, maps a side-less modifier onto the left + // key: macOS has no keycode meaning "either Shift". + let vk = KeyState.normalise(vk: data.virtualKey) guard let keycode = keycode(for: vk, flags: data.flags) else { dropped &+= 1 return diff --git a/Sources/MWBMac/KeyState.swift b/Sources/MWBMac/KeyState.swift index 135dc49..2a8558e 100644 --- a/Sources/MWBMac/KeyState.swift +++ b/Sources/MWBMac/KeyState.swift @@ -89,6 +89,10 @@ public struct KeyState: Sendable { /// (`InitAndCleanup.cs:225-229`) and the lock-chord replay /// (`InputHook.cs:485-512`) both send them, and macOS has no key that means /// "either Shift". Mapping to the left key is the only thing we can post. + /// + /// **Posting path only.** `process(vk:isUp:lockMachine:)` must be given the + /// raw wire VK: Windows' chord switches are side-specific, so the side-less + /// forms deliberately change nothing there (§10.5 note 3). public static func normalise(vk: Int32) -> Int32 { switch vk { case shift: return KeyMap.leftShift @@ -122,6 +126,18 @@ public struct KeyState: Sendable { /// Updates the chord state for one incoming key and says what the receiver /// must do about it. + /// + /// `vk` is the **raw** virtual key off the wire, not a normalised one. + /// `InputProcessKeyEx` matches side-specific VKs only — `VK.LSHIFT`/ + /// `VK.RSHIFT`, `VK.LCONTROL`/`VK.RCONTROL`, `VK.LMENU`/`VK.RMENU`, + /// `VK.LWIN`/`VK.RWIN` in both its switches (`InputSimulation.cs:372-403` + /// for the `LLKHF.UP` half, `:418-438` for the down half) — so the side-less + /// `VK_SHIFT`/`VK_CONTROL`/`VK_MENU` a lock-chord replay puts on the wire + /// (`InputHook.cs:485-512`) hit `default` and never move `shiftDown`/ + /// `ctrlDown`/`altDown`. `Common.HotkeyMatched` (`:406`) sees the raw code + /// too. Normalising before this point would let a replayed chord match lock + /// hotkeys a Windows peer would not match from the same bytes + /// (docs/protocol.md §10.5 note 3). public mutating func process(vk: Int32, isUp: Bool, lockMachine: Hotkey) -> KeyDecision { guard !isUp else { releaseChord(vk)