如何在 VSCode 中将一个键绑定到多个命令

How to bind one key to multiple commands in VSCode

我正在尝试让密钥 Ctrl+UpArrow 执行两个命令 cursorUpscrollLineUp.

我希望这会起作用,但它不起作用:

{
  "key": "ctrl+up",
   "command": ["cursorUp", "scrollLineUp"], // This doesn't work
   "when": "editorTextFocus"
}

如何在 VSCode 中执行此操作?

目前无法做到这一点,但已跟踪相应的功能请求 here. However you should take a look to the macros extension。它使您能够将不同的命令链接到单个自定义命令。然后可以将此自定义命令绑定到热键。 在您的情况下,您可以将此添加到您的 settings.json:

"macros": {
    "myCustomCommand": [
        "cursorUp",
        "scrollLineUp"
    ]
}

然后将您的自定义热键添加到 keybindings.json:

{
  "key": "ctrl+up",
  "command": "macros.myCustomCommand"
}

a new way 无需扩展即可实现此目的:

  1. 运行“任务:打开用户任务”命令创建或打开用户级任务文件。

  2. 将命令定义为单独的任务,如下所示:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "ctrlUp1",
            "command": "${command:cursorUp}"
        },
        {
            "label": "ctrlUp2",
            "command": "${command:scrollLineUp}"
        },
        {
            "label": "ctrlUpAll",
            "dependsOrder": "sequence",
            "dependsOn": [
                "ctrlUp1",
                "ctrlUp2"
            ],
            "problemMatcher": []
        }
    ]
}
  1. 在你的 keybindings.json:
{
    "key": "ctrl+up",
    "command": "workbench.action.tasks.runTask",
    "args": "ctrlUpAll",
    "when": "editorTextFocus"
}

(为便于阅读而选择的“ctrlUpNNN”标签格式,任务标签可以是任何内容)。