# ClaudeDesk **Let a coding agent see and drive your Windows desktop through a browser, so it can debug GUI applications the same way it debugs web pages.** A small HTTP bridge: pick a monitor or a window, watch it live in the browser, and click, drag, scroll and type into it — from the web page by hand, or from an agent through a JSON API. No build step, no browser extension, no kernel driver. > ⚠️ **This drives the real cursor and the real keyboard focus.** It is not a background > automation tool. While it runs, leave the machine alone or you will fight it for the > pointer. [Why that is unavoidable](#what-it-deliberately-does-not-do) is documented below — > the short version is that a Windows session has exactly one cursor, and every alternative > was tried and measured. --- ## Install Windows 10/11, Python 3.9+. ```bash git clone https://github.com/Phantivia/ClaudeDesk.git cd ClaudeDesk pip install -r requirements.txt ``` ## Run ```bash run.bat ``` Open . Interactive API docs live at `/api/docs`. The server binds to loopback only. It has **no authentication**, and anything that can reach the port can drive your machine — do not expose it. ## What you get | | | | --- | --- | | **See** | Live MJPEG of any monitor or any window. Adjustable fps and resolution. Window capture works even when the window is in the background or completely occluded. | | **Click** | Left / right / middle, double-click, drag, scroll wheel, with ctrl / shift / alt modifiers. | | **Type** | Literal text (layout-independent) and key combos like `ctrl+shift+p`. Named macros, editable from the page and persisted to `keymap.json`. | | **Manage** | Focus, move, resize, minimise, maximise, pin-on-top, close windows. Launch programs and wait for their window to appear. | | **Automate** | Everything above as a JSON API, plus `window.rig.*` helpers inside the page. | ## The coordinate contract Every input call takes a `source` plus `x` / `y` **in that source's own frame pixels**, where `(0, 0)` is the top-left of what you see: ```jsonc POST /api/click { "source": "window:461856", "x": 300, "y": 200 } ``` A source is `monitor:\\.\DISPLAY2` or `window:`. The server re-resolves the source's screen origin on every call, so a window that gets moved or resized keeps working — callers never do screen-coordinate arithmetic. Stream scaling does not affect click accuracy either, because the UI maps display pixels through the source's real dimensions rather than the encoded image size. ## API | Method | Path | | | --- | --- | --- | | `GET` | `/api/sources` | monitors + windows, each with a ready-made `source` string | | `GET` | `/api/state?source=` | cursor, foreground window, source geometry, keymap | | `GET` | `/api/frame?source=` | a single JPEG | | `GET` | `/api/stream?source=&fps=&max_width=` | MJPEG, drop straight into `` | | `POST` | `/api/snapshot` | writes a PNG to `captures/` and returns the path | | `POST` | `/api/click` | `source, x, y, button, count, modifiers, focus` | | `POST` | `/api/move` · `/api/drag` · `/api/scroll` | | | `POST` | `/api/key` | `keys:"ctrl+shift+p"` or `macro:"save"` | | `POST` | `/api/type` | literal text | | `GET`/`PUT` | `/api/keymap` | named macros | | `POST` | `/api/window/{focus,move,close,minimize,maximize,restore,topmost}` | | | `POST` | `/api/launch` | `command`, optional `wait_title` | ## Driving it from an agent ```bash curl -s http://127.0.0.1:8770/api/sources ``` ```powershell $b = 'http://127.0.0.1:8770' $body = @{ source = 'window:461856'; x = 300; y = 200 } | ConvertTo-Json Invoke-RestMethod "$b/api/click" -Method Post -ContentType 'application/json' -Body $body ``` To *look* at something, `POST /api/snapshot` and read the returned PNG path with your file tools. That beats screenshotting the browser: no scaling step, no resolution loss. From inside the page — e.g. an agent with a JavaScript execution tool — `window.rig` wraps the same calls against the current source: `rig.click(x, y)`, `rig.key('ctrl+s')`, `rig.type('hello')`, `rig.drag(x1, y1, x2, y2)`, `rig.snapshot()`, `rig.setSource(id)`. `CLAUDE.md` in the repo root is written for coding agents — point one at this directory and it has the contract, the API and the failure modes without any further explanation. ## Layout ``` server.py FastAPI app: routing, source resolution, coordinate translation selftest.py end-to-end verification — run after touching desktop/ keymap.json named key macros desktop/ capture.py monitor + window capture, encoding inputs.py SendInput mouse and keyboard, key table, combo parser winmgr.py focus, geometry, lifecycle, launching web/index.html the whole UI, no build step ``` ## Verifying a change ```bash python selftest.py ``` Launches a throwaway Tk window **on a non-primary monitor**, drives it only through the HTTP API, and asserts that what the window received matches what was asked for — click accuracy to the pixel, modifier state on combos, drag intermediates, wheel direction. Run it after touching anything in `desktop/`: coordinate bugs are silent, nothing errors, the click just lands somewhere else. ``` PASS click accuracy want (-2010,161) got (-2010,161) err=0px PASS key combo ctrl+s [('Control_L', 0), ('s', 4)] PASS drag 24 intermediate moves, ended at [-1860, 261] 12/12 passed ``` Run it on an **idle** machine. The suite shares one cursor and one keyboard focus with whoever is sitting there, so a stray click mid-run makes unrelated assertions fail. It checks for a moving cursor up front and says so, because the obvious reading of a failure is "the code broke" and that sends you hunting a bug that is not there. ## Things that will bite you Measured on real hardware while building this. If you write your own version, these are the traps: - **`MOUSEEVENTF_VIRTUALDESK` is mandatory.** Without it, absolute mouse coordinates are normalised against the *primary* monitor only, so clicks meant for a secondary display land somewhere else entirely — and nothing reports an error. Worst-case error measured across three monitors: **1px with the flag, 2553px without**. - **Capture needs `PrintWindow(PW_RENDERFULLCONTENT)`.** Plain `BitBlt` and `PrintWindow(0)` return solid black for anything GPU-composited — Chromium, Electron, UWP, WinUI. The FULLCONTENT path also captures backgrounded and fully occluded windows. - **Set DPI awareness before any window query.** `desktop/__init__.py` calls `SetProcessDpiAwarenessContext(PER_MONITOR_AWARE_V2)` at import time. - **Drags must glide.** Apps that track `WM_MOUSEMOVE` ignore a cursor that teleports from the press point to the release point; the drag never starts. Interpolate. - **Capture is the bottleneck, encoding is not.** A 2560×1440 monitor costs ~56 ms to grab and ~3 ms to JPEG-encode, so ~15 fps end to end. Capturing a single window is ~3× cheaper than a whole monitor. Raising quality is nearly free; raising source resolution is not. - **Minimised windows cannot be captured.** Restore first. ## What it deliberately does not do Operate without disturbing you. That was the original goal, and it is not achievable for arbitrary applications on a single Windows session. Every route was implemented and measured: | Approach | Result | | --- | --- | | `PostMessage` / `SendMessage` mouse messages | **Ignored.** Tested with correct child-window addressing, correct client coordinates, the full `WM_NCHITTEST` → `WM_MOUSEACTIVATE` → `WM_SETCURSOR` sequence, sync and async, foreground and background. Tk received nothing; WinUI received nothing. Modern toolkits do not trust coordinates handed to them in a message. | | A separate `CreateDesktop` object | **Refused.** The process runs there fine, but `GetCursorPos` returns nothing, `GetForegroundWindow` returns 0, and `SendInput` fails with `ACCESS_DENIED`. Non-input desktops do not accept injection. (Capture *does* work there.) | | `InjectTouchInput` | **Promoted back to mouse.** Coordinates arrive exactly, but `DefWindowProc` converts touch into mouse events, so the cursor jumps and focus is stolen anyway. Avoiding that requires the *target* app to handle `WM_POINTER` itself — no help for arbitrary software. | | `SendInput` | **Works, and is global.** One cursor, one focus. This is what ClaudeDesk uses. | UI Automation can invoke *controls* without touching the cursor, but it is control-level, not coordinate-level — no canvases, no games, no custom-drawn UI. Genuine isolation requires a separate session: a VM, a second logged-in session, or Windows Agent Workspace once it ships. (On Windows 11 Home there is no Hyper-V, no Windows Sandbox and no RDP hosting, so a third-party VM is the only local option.) If you need unattended automation, run the target in a VM and point ClaudeDesk at that. ## License MIT — see [LICENSE](LICENSE).