# Desktop Input (HID)

> 模拟键盘/鼠标输入。

Source: https://docs.sommatic.ai/zh/nodes/io.desktop.input/

- 标识: `io.desktop.input`
- 类别: 输入与输出
- 版本: 1.0.0
- 状态: 可用
- 执行方式: async

模拟键盘/鼠标输入。

## 配置

- `type` (string, 必填): 默认值: `keyboard`. 可选值: `keyboard`, `mouse`.
- `action` (string, 必填): 可选值: `type`, `click`, `move`, `keyTap`.
- `payload` (object, 可选): { text, x, y, key, modifier }

## 输出端口

- `out` (默认): 已模拟输入操作
- `error`: 模拟失败

## 输入契约

- `type` (string, 必填)
- `action` (string, 必填)
- `payload` (object, 可选)

## 输出契约

- `success` (boolean, 可选)

## 在表达式中

后续节点可通过 `$("Desktop Input (HID)").item.json.<路径>` 读取此节点输出中的内容（请把 `Desktop Input (HID)` 换成你的节点标签）。**Editor** 标记工作流编辑器在 Previous Nodes 中提示的路径（输出契约）；**Worker** 标记 worker 实际通过 `out` 返回的路径，依据“端口”一节所示的输出。

- `$("Desktop Input (HID)").item.json.success` (boolean) = `true`


## 工作原理

在运行 worker 的机器上模拟一次键盘或鼠标操作（输入文本、按键、移动指针或左键单击）。

1. 检查原生输入模块是否已加载；若未加载，则不执行任何操作直接从 `error` 输出。
2. 解析配置。`type`（默认 `keyboard`）和 `action` 缺失时回退到输入 payload 中的同名键。
3. 从 `payload.payload`（不存在时从 `payload`）读取操作参数 `text`、`x`、`y`、`key` 和 `modifier`。若节点配置中没有 `payload`，则 `payload` 即为输入数据。
4. 键盘：`type` 输入 `text`；`keyTap` 按下 `key`，可选 `modifier`（字符串或数组）。鼠标：`move` 移动到（`x`，`y`），默认为 0；`click` 在当前位置左键单击。
5. 从 `out` 返回 `{ success: true }`。

## 在工作流中

### 节点

```json
{
  "node_id": "node-type-reference",
  "label": "Type reference",
  "operator_slug": "io.desktop.input",
  "operator_version": "1.0.0",
  "config": {
    "type": "keyboard",
    "action": "type",
    "payload": {
      "text": "{{ $json.reference }}"
    }
  }
}
```

### Envelope

```json
{
  "ok": true,
  "data": {
    "success": true
  },
  "port": "out"
}
```

## 输出端口 (代码中的行为)

### `out`

操作已执行。当 `type`/`action` 组合不匹配任何操作时也会触发，此时未模拟任何输入。

```json
{
  "success": true
}
```

### `error`

原生输入模块不可用，或操作失败（例如按键名称无效）。

```json
{
  "error": "RobotJS Native Module not available in this environment"
}
```

## 代码中的行为

- `action`: 有效组合：`keyboard` + `type`/`keyTap`，`mouse` + `move`/`click`。其他组合不执行任何操作，但仍从 `out` 输出。
- `payload`: 包含操作参数 `{ text, x, y, key, modifier }`。嵌套的 `payload.payload` 优先。`click` 忽略 `x` 和 `y`，请先移动指针。

## 错误

| 消息 | 端口 | 原因与处理 |
| --- | --- | --- |
| `RobotJS Native Module not available in this environment` | `error` | 主机无法加载原生输入库（无桌面会话或平台不受支持）。请在有桌面会话的机器上运行该节点。 |
| `<input library error message>` | `error` | 输入库拒绝了参数，通常是 `keyTap` 缺少 `key` 或按键未知。请传入有效的按键名称。 |

## 说明与限制

- 输入会注入运行 worker 的主机当前桌面会话，由获得焦点的窗口接收。
- 一个节点只执行一个操作。若需操作序列，请串联多个节点（例如先 `move`，再 `click`，再 `type`）。


## 契约（JSON Schema）

### All (`*.definition.js`)

```json
{
  "slug": "io.desktop.input",
  "name": "Desktop Input (HID)",
  "category": "io",
  "version": "1.0.0",
  "description": "Simulate Keyboard/Mouse input.",
  "capabilities": [
    "hid",
    "keyboard",
    "mouse",
    "desktop"
  ],
  "config_schema": {
    "type": "object",
    "properties": {
      "type": {
        "type": "string",
        "enum": [
          "keyboard",
          "mouse"
        ],
        "default": "keyboard"
      },
      "action": {
        "type": "string",
        "enum": [
          "type",
          "click",
          "move",
          "keyTap"
        ]
      },
      "payload": {
        "type": "object",
        "description": "{ text, x, y, key, modifier }"
      }
    },
    "required": [
      "type",
      "action"
    ]
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "type": {
        "type": "string"
      },
      "action": {
        "type": "string"
      },
      "payload": {
        "type": "object"
      }
    },
    "required": [
      "type",
      "action"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "success": {
        "type": "boolean"
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Done",
      "description": "Input action simulated",
      "is_default": true,
      "supports_condition": true
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Simulation failed",
      "is_default": false,
      "supports_condition": false
    }
  ],
  "execution_mode": "async",
  "required_resources": [],
  "compatible_worker_pools": [
    "desktop-agent"
  ],
  "tags": [
    "desktop",
    "hid"
  ],
  "is_stateful": false,
  "publisher": {
    "identity": "org_link_loom",
    "profile": {
      "name": "Link Loom",
      "verified": true,
      "url": "https://me.veripass.com/link-loom",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "properties": {
    "type": {
      "type": "string",
      "enum": [
        "keyboard",
        "mouse"
      ],
      "default": "keyboard"
    },
    "action": {
      "type": "string",
      "enum": [
        "type",
        "click",
        "move",
        "keyTap"
      ]
    },
    "payload": {
      "type": "object",
      "description": "{ text, x, y, key, modifier }"
    }
  },
  "required": [
    "type",
    "action"
  ]
}
```

### input_contract_schema

```json
{
  "type": "object",
  "properties": {
    "type": {
      "type": "string"
    },
    "action": {
      "type": "string"
    },
    "payload": {
      "type": "object"
    }
  },
  "required": [
    "type",
    "action"
  ]
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "success": {
      "type": "boolean"
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "out",
    "label": "Done",
    "description": "Input action simulated",
    "is_default": true,
    "supports_condition": true
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Simulation failed",
    "is_default": false,
    "supports_condition": false
  }
]
```
