简体   繁体   English

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

[英]How to bind one key to multiple commands in VSCode

I'm trying to make the key Ctrl+UpArrow execute both commands cursorUp and scrollLineUp .我试图让键Ctrl+UpArrow cursorUp执行两个命令cursorUpscrollLineUp

I was hoping that this would work, but it doesn't:我希望这会起作用,但它没有:

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

How do I do that in VSCode?我如何在 VSCode 中做到这一点?

This is currently not possible, but the corresponding feature request is tracked here .这目前是不可能的,但在此处跟踪了相应的功能请求。 However you should take a look to the macros extension .但是,您应该查看宏扩展名 It enables you to chain different commands to a single custom command.它使您能够将不同的命令链接到单个自定义命令。 This custom command then can be bound to a hotkey.然后可以将此自定义命令绑定到热键。 In your case you could add this to your settings.json :在您的情况下,您可以将其添加到settings.json

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

And then add your custom hotkey to the keybindings.json :然后将您的自定义热键添加到keybindings.json

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

There's a new way to achieve this without an extension:一种无需扩展即可实现此目的的新方法

  1. Run "Tasks: Open User Tasks" command to create or open a user level tasks file.运行“任务:打开用户任务”命令以创建或打开用户级任务文件。

  2. Define commands as separate tasks, like so:将命令定义为单独的任务,如下所示:

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

("ctrlUpNNN" label format chosen for readability, task labels can be anything). (为可读性选择的“ctrlUpNNN”标签格式,任务标签可以是任何东西)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM