Skip to content

v1.3.1

Latest

Choose a tag to compare

@rpdg rpdg released this 10 Nov 03:02

winput v1.3.1

This release adds a task-level input API with a fixed backend (NewInput), read-only target and HID probes, clipboard paste, Window.ClientInfo, and optional intervals for Press and Type. It also reworks screen capture, guards HID window keys with a foreground check, rejects unsafe lookups, sizes, and coordinates, and hardens MCP request handling.

⚠️ Upgrade note: a few changes can affect existing code. See Changed below.

  • The exported variable window.ProcGetProcessDpiAwarenessCtx is removed. Use window.ProcGetDpiAwarenessContextForProcess. The old variable pointed at a user32 export that does not exist, so it never worked.
  • Press, Type, Window.Press, and Window.Type now take an optional interval ...time.Duration. Existing calls still compile, but code that stores these functions in a variable of the old function type must be updated.
  • Screen capture now checks the DPI awareness of the calling thread, not the process.
  • CaptureOptions.MaxMemoryMB is now a peak budget (DIB plus Go image, 2 × w × h × 4 bytes). The default is 1000 MB, which allows the same pixel count as the old 500 MB default. An explicit positive value now allows half as many pixels as before.
  • A capture can return a non-nil image and a non-nil error when the pixels were copied but a GDI release failed. The image is usable.
  • CaptureOptions.PreserveAlpha is deprecated and has no effect. Alpha is always 255.
  • On BackendHID, window key methods (Window.KeyDown, Press, PressHotkey, Type) now send a key-down only while the window's top-level ancestor is the foreground window. Otherwise they return ErrTargetNotForeground (Type stops at that character).
  • FindByProcessName now returns windows from every process with that executable name, not only the first one.
  • Message-backend mouse calls now return ErrInvalidMouseCoordinate for coordinates outside int16 instead of clipping them.

Added

  • Task-level input API: NewInput(backend) returns an *Input bound to BackendMessage or BackendHID at construction. A later SetBackend does not change it.
    • Input.ForWindow(w) returns a WindowInput with Click, ClickRight, ClickMiddle, DoubleClick, Move, MoveRel, Scroll, KeyDown, KeyUp, Press, PressHotkey, and Type.
    • Input.Desktop() returns a DesktopInput with the screen-coordinate and windowless calls: MoveMouseTo, MoveMouseToExact, MoveMouseBy, ClickMouse, ClickMouseAt, ClickRightMouseAt, ClickMiddleMouseAt, DoubleClickMouseAt, MouseDown, MouseUp, ScrollWheel, KeyDown, KeyUp, Press, PressFor, PressHotkey, Type, and PasteByClipboard.
    • New errors: ErrInputInvalid (zero or nil executor), ErrInvalidBackend.
  • Probes:
    • ProbeTarget(w) returns a TargetProbe with facts about a window (ProcessName, TargetClass, IntegrityRelation, CallerUIAccess) and derived Warnings (WarnUIPILikelyBlocked, WarnIntegrityUnknown, WarnUIAccessUnknown). It does not initialize HID or recommend a backend. It holds one process handle for the whole probe and returns ErrTargetChanged if the window's identity changes during the probe.
    • ProbeHID() returns an HIDStatus (HIDInitialized, HIDDLLUnavailable, HIDDriverUnavailable, HIDDevicesUnavailable, HIDUnknown) and leaves the global backend unchanged.
  • PasteByClipboard(text): pastes text into the foreground window with Ctrl+V and then restores the previous CF_UNICODETEXT contents. This is not a full clipboard backup: only Unicode text is put back. New errors: ErrEmptyPasteText, ErrClipboardFailed. DesktopInput.PasteByClipboard does the same on the executor's fixed backend.
  • Window.ClientInfo(): returns the client area as a screen rectangle (x, y, width, height).
  • Optional intervals: Press(k, interval) sets the hold time between key-down and key-up. Type(text, interval) sets the gap between characters. Both apply to the package-level and Window methods on both backends. A negative value or more than one value returns ErrInvalidInterval.
  • screen.CaptureRegionWithOptions: CaptureRegion with custom CaptureOptions.
  • New error sentinels: ErrTargetNotForeground, ErrInvalidLookupString, ErrInvalidMouseCoordinate, ErrInvalidInterval, and the probe and paste errors listed above. hid adds ErrLibraryNotFound, ErrSymbolsMissing, and ErrDevicesUnavailable, and hid/interception adds ErrSymbolsMissing, so HID init failures can be matched with errors.Is.
  • New lower-level functions: keyboard.TypeWithInterval and keyboard.DefaultTypeGap, window.SwapUnicodeText, window.FindPIDsByName, window.FindByPIDs, window.AncestorRoot, plus clipboard, process, token, and DPI procs in window.

Changed

  • Screen capture (screen package):
    • Each capture locks the calling goroutine to one OS thread, from the DPI check through GDI release, and flushes the GDI batch before reading pixels.
    • CaptureRegion now blits only the part of the requested rectangle that lies inside the virtual desktop, instead of capturing the whole desktop and cropping it. The image starts at the top-left of that intersection, so do not pass a CaptureRegion image to ImageToVirtual.
    • The DPI check reads the calling thread's DPI awareness, and falls back to the process when the thread APIs are missing. The error text says whether the process or only this thread is not per-monitor aware.
    • MaxMemoryMB is a peak budget with a 1000 MB default (see the upgrade note). The error message reports the peak size.
    • GDI failures include the Win32 error code and wrap the syscall.Errno with %w.
    • If a GDI release fails after the pixels were copied, the image is returned together with the cleanup error.
    • PreserveAlpha is deprecated. Every pixel has alpha 255.
  • HID window keys: on BackendHID, window key-downs are sent only while the target's top-level ancestor is the foreground window. Otherwise ErrTargetNotForeground is returned and keys that the call already pressed are released. On BackendMessage, modifier hotkeys now activate the top-level ancestor instead of a child hwnd.
  • FindByProcessName collects windows from every process with the given name, in Toolhelp snapshot order.
  • The message-backend Type no longer sleeps after the last character, so it returns about 30 ms earlier.
  • MCP server:
    • Requests are capped at 16 MiB for both framed and line-delimited input. Header blocks are capped at 64 lines and 8 KiB per line.
    • Missing or explicit-null required arguments are rejected, and press_hotkey requires 1–8 keys.
    • find_window with process_name covers every process with that name.
    • New error mappings: target_not_foreground, target_not_visible. An embedded NUL in a lookup string, an unsupported key, an out-of-range mouse coordinate, and a point outside the virtual desktop map to validation_error.
    • Serve returns when its context is cancelled, even while it is waiting for input.

Fixed

  • Clipboard writes now fail with ErrClipboardFailed when GlobalUnlock reports the block as still locked. Before, the write could be treated as done, and Ctrl+V would paste the old clipboard contents.
  • An embedded NUL in a window title or class now returns ErrInvalidLookupString (which still matches ErrWindowNotFound) instead of being passed to FindWindow as a wildcard.
  • Window text reads reject reported lengths above the 16 MiB limit before allocating.
  • Window.MoveRel returns an error, without moving, when the cursor position plus the delta overflows int32.
  • hid.SetLibraryPath no longer races with loading and unloading the Interception library.
  • EnumWindows and EnumDisplayMonitors each register one callback for the process, instead of creating a new callback on every call. This avoids running out of Go's limit of 2000 callbacks.
  • screen.Monitors returns an error when EnumDisplayMonitors fails, instead of an empty list.
  • UI Automation: the VARIANT is now 24 bytes on 64-bit, and the 386 build passes it by value to CreatePropertyCondition.
  • The DPI proc now loads GetDpiAwarenessContextForProcess, the function Windows actually exports.

Internal

  • Added tests for the input executor and facade backend isolation, the probes and their handle lifecycle, clipboard paste and restore, intervals, capture limits and region intersection, thread DPI, lookup and coordinate validation, the UIA VARIANT layout, and the MCP framing reader and call validation.
  • Documentation: API.md, API_CN.md, README.md, README_CN.md, and docs/mcp-tools.md are updated for the new APIs and behavior. Added the design document docs/backend-probe.md.

Full Changelog: v1.3.0...v1.3.1