Scripting and live keys
Programs on your PC can change what your keys show (a clock, stock prices, build status, a progress ring, and an animation) and can listen for key presses.
The local API is off until you turn it on. It can be reached only from your PC, and every request needs a token. Scripts can set and clear key images and listen to events. They can read and change what keys do, and add, change, and remove keys, only if you also tick Let scripts and assistants change what keys do on the same screen, which is off by default. Key changes happen on your phone while it is connected, and you can undo each one in the phone's editor.
Turn it on
- In the KMacros PC app, open Scripts & API and tick Enable local API.
- Copy the API token (the eye button shows it, the Copy button copies it).
- Give a key an API name in its details on the right, for example
clock. Scripts can use the name or the key's ID. The same page's Reference shows every call with examples that already contain your token and port.
The API listens on 127.0.0.1:47200 by default (the port can be changed). Every request needs Authorization: Bearer YOUR_TOKEN. Regenerating the token disconnects running scripts and clears the images they set.
Prefer to talk to an AI assistant? KMacros also has a built-in MCP server that does the same things.
If you only need a key to start an AutoHotkey, PowerShell, or other script, you do not need the API. Use a Run a program step instead.
Display keys
On the phone, a key of type Display can't be pressed and has no label. It exists to show what your scripts send, filling the key with only a thin margin. Give it an API name and push images to it.
Calls
All paths start with /api/v1. {key} is an API name or a key ID.
| Call | What it does |
|---|---|
GET/status | The linked phones, whether each is connected, and whether it is in play, edit, or background mode. |
GET/keys | Every key: id, API name, label, type, keyboard, whether it is visible now, and the exact pixel sizes to draw at. |
PUT/keys/{key}/image | Set an image (PNG, JPEG or WebP; GIF and animated WebP too). The body is the raw image. |
POST/keys/{key}/animation | Upload your own animation as frames plus durations. The phone plays it smoothly by itself. |
DELETE/keys/{key}/image | Put the key back to normal. |
DELETE/images | Put every key you set back to normal. |
GET/doctor | The Connection Doctor's results (firewall, network type, port). POST /doctor/check runs the checks again. |
WS/events | A WebSocket of events: where keys are drawn and how big, key presses, toggles, and slider moves, and phones connecting. |
WS/stream | A WebSocket for fast frames, for data that changes many times a second. |
Options when setting an image
| Option | Meaning |
|---|---|
fit | icon replaces just the icon; face covers the whole key and hides the label. (Display keys always fill the key.) |
tint | true paints the image's shape in the key's skin colors, so it matches every skin. |
alt | What the phone's screen reader says for the key. |
ttl | Seconds until the key puts itself back. Use it for live data, so a crashed script leaves nothing stale. |
Examples
Set a status picture, then put the key back:
curl.exe -X PUT -H "Authorization: Bearer YOUR_TOKEN" -H "Content-Type: image/png" ^
--data-binary "@status.png" "http://127.0.0.1:47200/api/v1/keys/build/image?fit=icon&ttl=120"
curl.exe -X DELETE -H "Authorization: Bearer YOUR_TOKEN" http://127.0.0.1:47200/api/v1/keys/build/image
A clock that matches every skin (Python with Pillow and requests):
import io, time, requests
from datetime import datetime
from PIL import Image, ImageDraw, ImageFont
API = "http://127.0.0.1:47200/api/v1"
H = {"Authorization": "Bearer YOUR_TOKEN", "Content-Type": "image/png"}
font = ImageFont.truetype("seguisb.ttf", 88)
while True:
img = Image.new("RGBA", (256, 256), (0, 0, 0, 0)) # transparent background
ImageDraw.Draw(img).text((128, 128), datetime.now().strftime("%H:%M"), font=font, fill="white", anchor="mm")
buf = io.BytesIO(); img.save(buf, "PNG")
# tint=true: drawn in the key's skin colors. ttl: the key reverts if this script dies.
requests.put(f"{API}/keys/clock/image?fit=face&tint=true&ttl=90&alt=Time", data=buf.getvalue(), headers=H)
time.sleep(60 - datetime.now().second)
Limits
- Images are scaled to fit the key (at most 512 × 512) and must be 128 KB or less per frame after KMacros re-encodes them.
- Up to 10 updates per second per key, and 30 per second in all. Over that, HTTP answers
429and streams keep only the latest frame. - Animations: up to 120 frames and 2 MB, each frame 16 to 10,000 ms. Prefer an animation to streaming for anything that repeats.
- Sliders can't show images. While no phone is connected, KMacros keeps the latest image and sends it when one connects, unless its
ttlhas run out. A phone puts a key back the moment it loses the connection to the PC.
Errors use plain codes: key.unknown (404), image.badFormat (400), image.tooLarge (413), image.unsupportedKey (422) and rateLimited (429).