# key_press

Press one key with optional modifiers, to the frontmost app. For text use `text_type`. Shortcuts: `key: "s", modifiers: ["meta"]`.

**In short:** Risk class [`control`](https://docs.cmdop.ai/reference/tools.md#risk-classes). Over MCP call `key_press`; over HTTP send `"tool": "key_press"` to [`POST /api/v1/call`](https://docs.cmdop.ai/reference/http/post-call.md).

## Arguments

Over MCP every tool also takes `machine`: the machine's name or id, optional when only one machine is online.

| Name | Type | Required | Meaning |
| --- | --- | --- | --- |
| `hold_ms` | `integer` | no | Hold the key this long, in ms (at most 5000). |
| `key` | `string` | yes | A key name: `a`, `Enter`, `Tab`, `Escape`, `ArrowDown`, `F5`, `Space`. One key; the modifiers go in `modifiers`, never as `"ctrl+s"`. |
| `modifiers` | `"shift" | "ctrl" | "alt" | "meta"[]` | no |  Default: `[]`. |
| `observe` | `"none" | "ui" | "image"` | no |  Default: `"none"`. |
| `repeat` | `integer` | no | Press this many times (default 1, at most 100). |

## Result

| Name | Type | Required | Meaning |
| --- | --- | --- | --- |
| `audit_id` | `string` | yes | Id of the audit line on the machine. |
| `dispatched` | `boolean` | yes |  |
| `foreground_changed` | `boolean` | yes | The frontmost app or window changed because of the action. |
| `observation` | `object` | no |  |
| `settled_ms` | `integer` | yes | How long the screen took to go quiet after the action, in milliseconds. |

## Examples

**MCP**

```json
{
  "name": "key_press",
  "arguments": {
    "machine": "<machine>",
    "key": "<key>"
  }
}
```

**HTTP**

```bash
curl -sS -X POST "https://<contour>.cmdop.ai/api/v1/call" \
  -H "Authorization: Bearer $CMDOP_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"machine":"<machine>","tool":"key_press","args":{"key":"<key>"}}'
```

## Limits

The call times out after 20 s (`timed_out`). Available since version 0.0.2. A refused or failed call returns one of the [error codes](https://docs.cmdop.ai/reference/errors.md).
