{"record":{"id":"cb1ccc66627f3906","repo":"Hmbown/CodeWhale","slug":"sendinput-inserted-0-of-1-events-win32-error-2","errorCode":null,"errorMessage":"SendInput inserted {0} of {1} events (Win32 error {2})","messagePattern":"SendInput inserted (.+?) of (.+?) events \\(Win32 error (.+?)\\)","errorType":"exception","errorClass":"InputDeliveryException","httpStatus":null,"severity":"error","filePath":"crates/tui/plugins/computer-use/src/backends/win32.mjs","lineNumber":45,"sourceCode":"  [StructLayout(LayoutKind.Sequential)] public struct INPUT { public uint type; public INPUTUNION u; }\n  public const uint MOVED = 0x0001, LEFTDOWN = 0x0002, LEFTUP = 0x0004, RIGHTDOWN = 0x0008, RIGHTUP = 0x0010,\n    MIDDLEDOWN = 0x0020, MIDDLEUP = 0x0040, WHEEL = 0x0800, HWHEEL = 0x1000, ABSOLUTE = 0x8000, VIRTUALKEY = 0x4000, KEYUP = 0x0002, UNICODE = 0x0004;\n  public static INPUT MouseInput(uint flags, int x, int y, uint data) {\n    return new INPUT { type = 0, u = new INPUTUNION { mi = new MOUSEINPUT { dx = x, dy = y, mouseData = data, dwFlags = flags, time = 0, dwExtraInfo = IntPtr.Zero } } };\n  }\n  public static INPUT KeyInput(ushort vk, ushort scan, uint flags) {\n    return new INPUT { type = 1, u = new INPUTUNION { ki = new KEYBDINPUT { wVk = vk, wScan = scan, dwFlags = flags, time = 0, dwExtraInfo = IntPtr.Zero } } };\n  }\n  public sealed class InputDeliveryException : InvalidOperationException {\n    public readonly uint Inserted;\n    public InputDeliveryException(uint inserted, int expected, int error)\n      : base(String.Format(\"SendInput inserted {0} of {1} events (Win32 error {2})\", inserted, expected, error)) { Inserted = inserted; }\n  }\n  public static void SendChecked(INPUT[] inputs) {\n    uint sent = SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));\n    if (sent != (uint)inputs.Length) {\n      int error = Marshal.GetLastWin32Error();\n      throw new InputDeliveryException(sent, inputs.Length, error);\n    }\n  }\n  public static void SendKey(ushort vk, uint flags) {\n    SendChecked(new INPUT[] { KeyInput(vk, 0, flags) });\n  }\n  public static void SendString(string text) {\n    // UTF-16 code units, including both halves of surrogate pairs. UNICODE\n    // requires wVk=0 and may only be combined with KEYUP, never SCANCODE.\n    foreach (char c in text) {\n      INPUT up = KeyInput(0, c, UNICODE | KEYUP);\n      try { SendChecked(new INPUT[] { KeyInput(0, c, UNICODE), up }); }\n      catch (InputDeliveryException failure) {\n        // A partial pair may leave only our down event inserted. Release it\n        // once, never replay text, and still report the original failure.\n        if (failure.Inserted == 1) {\n          try { SendChecked(new INPUT[] { up }); }\n          catch (Exception cleanup) {\n            throw new AggregateException(\"Unicode input failed: \" + failure.Message + \"; key-up recovery failed: \" + cleanup.Message, failure, cleanup);","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/tui/plugins/computer-use/src/backends/win32.mjs#L27-L63","documentation":"The Win32 helper SendChecked calls the native SendInput API and verifies it inserted all submitted events. If SendInput reports fewer events than were passed in, it throws InputDeliveryException with the inserted/expected counts and the Win32 error code. This detects partially applied input streams rather than letting the caller assume all keystrokes landed.","triggerScenarios":"Any input injection (SendKey/SendString/pointer events) when the desktop or window station is not interactive (locked screen, secure desktop, UAC prompt, disconnected RDP session), or when another process with higher privileges blocks injection (UIPI).","commonSituations":"Running the automation from a service or scheduler session; screen locked during a run; trying to send input to an elevated window from a non-elevated process; Win32 error 5 (access denied) or 0 mapping to unexpected states.","solutions":["Run the automation in the same interactive, elevated (or higher) session as the target window — check the Win32 error in the message (usually 5 = access denied)","Unlock the screen / connect the RDP session (shadow session) before sending input","Retry SendChecked for transient failures, and split large INPUT arrays into smaller batches","If injecting into elevated UI, run your host process elevated or adjust UIPI (change window integrity only if you own the policy)"],"exampleFix":"// before\nSendKey(vk, flags);\n// after\ntry { SendKey(vk, flags); }\ncatch (InputDeliveryException e) {\n  if (e.Inserted == 1) SendChecked(new INPUT[] { up }); // release partial key-down\n  throw;\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { SendChecked(inputs); }\ncatch (InputDeliveryException e) {\n  if (e.Inserted == 1) { /* release the single inserted event */ }\n  if ((uint)e.Data[\"Win32Error\"] == 5) throw new InvalidOperationException(\"input blocked: run elevated / interactive\", e);\n  throw;\n}","preventionTips":["Keep the automation process in the interactive, unlocked desktop session","Match elevation level with the target window (UIPI)","Keep RDP sessions connected (or shadow them) during input injection","Batch inputs and verify counts after each batch"],"tags":["win32","sendinput","input-injection","windows","native-interop"],"backgroundTag":"input-injection-failed","analyzedSha":"73e0f67d83c59909b571efdfc88c4bc28c309cb1","analyzedAt":"2026-09-22T01:30:00.501Z","contentChangedAt":"2026-09-22T01:30:00.501Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}