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)perInjector, withuserData = InputTypes.injectedEventTagso every event it creates carries the tag ineventSourceUserDataand the tap'sRealDatacheck (InputHook.cs:213-220) passes it through. Events are posted with.post(tap: .cghidEventTap), the closest thing toSendInput. - Keyboard (
InputSimulation.cs:108-157):wVk→KeyMap.keycode(forVK:); unknown VKs are dropped and counted (Windows wouldMapVirtualKeythem; we have no keycode to post).LLKHF.UP→ key up. Modifier VKs (LSHIFT…RWIN,SHIFT/CONTROL/MENUgeneric forms mapped to the left key) post as key events for the modifier keycode, which macOS delivers asflagsChanged; the injector keeps aKeyStateof held modifiers and setsevent.flagson every posted key and mouse event from it, otherwise ⌘C from a Windows keyboard arrives as a bare C.EXTENDEDis informational here (the keycode already distinguishes keypad Enter etc.). Repeats arrive as repeated downs and are posted as such withkeyboardEventAutorepeat = 1after the first. The 10 ms sleep after key-up (Event.cs:268-271) is the sender's, not ours. - Caps Lock:
VK_CAPITALdown+up toggles; post the keycode pair and let the system toggle the LED state. - Mouse (
InputSimulation.cs:160-219):dwFlagsis aWM_*id.WM_MOUSEMOVEwith absolute coordinates →Coordinates.fromUniversalonto 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*MouseDraggedtype (macOS needs it for drags; Windows does not distinguish). Buttons: L/R/M down/up → the CG button types withmouseEventButtonNumber0/1/2;WM_XBUTTON*withWheelDelta1/2 →.otherMouseDown/Upwith button number 3/4. Windows drops*DBLCLK(noMOUSEEVENTFcase); double clicks arrive as two downs, so click count is ours: consecutive downs of the same button withinNSEvent.doubleClickIntervaland 4 px setmouseEventClickState2, 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_Lwith Win held) locks this Mac by running/System/Library/CoreServices/Menu Extras/User.menu/Contents/Resources/CGSession -suspendrather 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 clearsKeyState. 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. NotSendablebeyond that queue. - Cursor bounds come from
ScreenLayoutpassed in with each packet; the injector never callsScreensitself.
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.