命令与快捷键绑定 API 允许扩展注册自定义命令,并将其与键盘快捷键关联。这样用户可以无需鼠标,快速触发操作。

基本用法

app.registerExtension({
  name: "MyExtension",
  // 注册命令
  commands: [
    {
      id: "myCommand",
      label: "我的命令",
      function: () => {
        console.log("命令已执行!");
      }
    }
  ],
  // 将快捷键与命令关联
  keybindings: [
    {
      combo: { key: "k", ctrl: true },
      commandId: "myCommand"
    }
  ]
});

命令配置

每个命令都需要 idlabelfunction

{
  id: string,              // 命令的唯一标识符
  label: string,           // 命令显示名称
  function: () => void     // 命令被触发时执行的函数
}

快捷键配置

每个快捷键都需要 combocommandId

{
  combo: {                 // 按键组合
    key: string,           // 主键(单个字符或特殊按键)
    ctrl?: boolean,        // 是否需要 Ctrl 键(可选)
    shift?: boolean,       // 是否需要 Shift 键(可选)
    alt?: boolean,         // 是否需要 Alt 键(可选)
    meta?: boolean         // 是否需要 Meta/Command 键(可选)
  },
  commandId: string        // 要触发的命令 ID
}

特殊按键

对于非字符按键,请使用以下值之一:

  • 方向键:"ArrowUp""ArrowDown""ArrowLeft""ArrowRight"
  • 功能键:"F1""F12"
  • 其他特殊按键:"Escape""Tab""Enter""Backspace""Delete""Home""End""PageUp""PageDown"

命令示例

app.registerExtension({
  name: "CommandExamples",
  commands: [
    {
      id: "runWorkflow",
      label: "运行工作流",
      function: () => {
        app.queuePrompt();
      }
    },
    {
      id: "clearWorkflow",
      label: "清空工作流",
      function: () => {
        if (confirm("确定要清空工作流吗?")) {
          app.graph.clear();
        }
      }
    },
    {
      id: "saveWorkflow",
      label: "保存工作流",
      function: () => {
        app.graphToPrompt().then(workflow => {
          const blob = new Blob([JSON.stringify(workflow)], {type: "application/json"});
          const url = URL.createObjectURL(blob);
          const a = document.createElement("a");
          a.href = url;
          a.download = "workflow.json";
          a.click();
          URL.revokeObjectURL(url);
        });
      }
    }
  ]
});

快捷键示例

app.registerExtension({
  name: "KeybindingExamples",
  commands: [
    /* 上述命令定义 */
  ],
  keybindings: [
    // Ctrl+R 运行工作流
    {
      combo: { key: "r", ctrl: true },
      commandId: "runWorkflow"
    },
    // Ctrl+Shift+C 清空工作流
    {
      combo: { key: "c", ctrl: true, shift: true },
      commandId: "clearWorkflow"
    },
    // Ctrl+S 保存工作流
    {
      combo: { key: "s", ctrl: true },
      commandId: "saveWorkflow"
    },
    // F5 运行工作流(备用)
    {
      combo: { key: "F5" },
      commandId: "runWorkflow"
    }
  ]
});

注意事项与限制

  • ComfyUI 核心中定义的快捷键无法被扩展覆盖。请在以下源码文件中查看核心快捷键:

  • 某些按键组合被浏览器保留(如 Ctrl+F 用于搜索),无法被覆盖

  • 如果多个扩展注册了相同的快捷键,行为未定义