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)