Files
mac_without_borders/docs/features/input-inject/README.md
T
2026-09-16 02:38:11 -07:00

5.7 KiB

input-inject

Goal

The Mac's side of InputSimulation.cs: turn trusted Keyboard and Mouse packets into CGEvents posted to the HID event tap, tagged so our own EventTap passes them through, with the click-count, held-key and cursor-bounds bookkeeping macOS needs and Windows gets for free from SendInput. Also the local reactions to remote chords (InputSimulation.cs:358-475): Win+L locks this Mac, Ctrl+Alt+Del is ignored, and releaseAll() — the ReleaseAllKeys of InitAndCleanup.cs:214-243 — that contracts C2 calls on every recovery.

Assumptions

  • One CGEventSource(stateID: .hidSystemState) per Injector, with userData = InputTypes.injectedEventTag so every event it creates carries the tag in eventSourceUserData and the tap's RealData check (InputHook.cs:213-220) passes it through. Events are posted with .post(tap: .cghidEventTap), the closest thing to SendInput.
  • Keyboard (InputSimulation.cs:108-157): wVk → KeyMap.keycode(forVK:); unknown VKs are dropped and counted (Windows would MapVirtualKey them; we have no keycode to post). LLKHF.UP → key up. Modifier VKs (LSHIFT…RWIN, SHIFT/CONTROL/MENU generic forms mapped to the left key) post as key events for the modifier keycode, which macOS delivers as flagsChanged; the injector keeps a KeyState of held modifiers and sets event.flags on every posted key and mouse event from it, otherwise ⌘C from a Windows keyboard arrives as a bare C. EXTENDED is informational here (the keycode already distinguishes keypad Enter etc.). Repeats arrive as repeated downs and are posted as such with keyboardEventAutorepeat = 1 after the first. The 10 ms sleep after key-up (Event.cs:268-271) is the sender's, not ours.
  • Caps Lock: VK_CAPITAL down+up toggles; post the keycode pair and let the system toggle the LED state.
  • Mouse (InputSimulation.cs:160-219): dwFlags is a WM_* id. WM_MOUSEMOVE with absolute coordinates → Coordinates.fromUniversal onto the desktop bounds (:160-173); with the relative bias → delta applied to the current pointer (Receiver.cs:147-153); the result is clamped to the desktop union. A move while a button is held posts the matching *MouseDragged type (macOS needs it for drags; Windows does not distinguish). Buttons: L/R/M down/up → the CG button types with mouseEventButtonNumber 0/1/2; WM_XBUTTON* with WheelDelta 1/2 → .otherMouseDown/Up with button number 3/4. Windows drops *DBLCLK (no MOUSEEVENTF case); double clicks arrive as two downs, so click count is ours: consecutive downs of the same button within NSEvent.doubleClickInterval and 4 px set mouseEventClickState 2, 3…, reset otherwise.
  • Wheel: WM_MOUSEWHEEL → CGEvent(scrollWheelEvent2Source:units: .line, wheelCount: 1, wheel1: WheelDelta / 120); WM_MOUSEHWHEEL → wheelCount: 2, wheel2:. Sign: Windows +120 is away from the user, which is CG line +1. The user's "natural scrolling" preference is applied by the system to posted events too; whether that matches what they expect from a remote mouse is a CP1 question, recorded in §10.5.
  • Remote chords (InputSimulation.cs:358-475): the injector tracks the remote's Win/Ctrl/Alt/Shift from the VKs it receives; Win+L (VK_L with Win held) locks this Mac by running /System/Library/CoreServices/Menu Extras/User.menu/Contents/Resources/CGSession -suspend rather than injecting the chord (which would open Spotlight-like shortcuts); Ctrl+Alt+Del is swallowed and logged; Ctrl+Alt+End (service start) is ignored (README D6).
  • releaseAll() posts key-up for every key and button the injector believes is down — a superset of Windows' fixed modifier list (InitAndCleanup.cs:214-243) because a lost key-up for a letter is as bad as for Shift — then clears KeyState. Rate limit of once per 2 s (:218) is not ported: recovery must always work (C2).
  • Threading: the injector is driven by the Engine actor and posts from its own serial DispatchQueue; posting is thread-safe but the bookkeeping is not, so all of it lives on that queue. Not Sendable beyond that queue.
  • Cursor bounds come from ScreenLayout passed in with each packet; the injector never calls Screens itself.

Handshakes

Consumed

  • input-capture: InputTypes.injectedEventTag, WindowsMessage, KeyFlag, KeyMap.keycode(forVK:), ScreenLayout, and the promise that tagged events are passed through untouched.
  • session-matrix: Coordinates.fromUniversal, the relative-bias decode.
  • wire-core: Packet.mouse / Packet.keyboard.

Promised (to app-shell)

public final class Injector {
  public init()
  public func inject(_ packet: Packet, layout: ScreenLayout)       // Keyboard or Mouse; anything else ignored
  public func releaseAll()                                          // C2; safe to call any time
  public func lockThisMac()                                         // CGSession -suspend
  public var droppedUnmappedKeys: UInt64 { get }
  public var remoteModifiers: ModifierState { get }                 // for the menu bar / debugging
}

Status / TODO

  • Sources/MWBMac/KeyState.swift: held keys/buttons, modifier flag word, click-count tracking, remote-chord state (InputSimulation.cs:358-475, InitAndCleanup.cs:214-243).
  • Sources/MWBMac/Injector.swift: the tagged source, keyboard and mouse posting per Assumptions, wheel, clamping, releaseAll, lockThisMac, Ctrl+Alt+Del handling (InputSimulation.cs:82-219, 440-475, Receiver.cs:147-153).
  • docs/protocol.md: append §10.5 with anything learned, cited.

Work log

QA log

Review