如何为 VSCode 中的扩展设置快捷键?

How do I set a keybinding for an extension in VSCode?

我正在使用 VSCode 编写 Swagger (OpenAPI) 规范,我想利用特定扩展来帮助编写该规范。

我安装的扩展程序没有为我提供方便调用它的键绑定。

如何添加键绑定?我试图通过单击文件->首选项->键盘快捷键并编辑 keybindings.json 文件来使其工作,但到目前为止没有成功。

我似乎必须找到扩展程序的命令,但我不知道在哪里可以找到它,在扩展程序摘要页面上似乎并不明显,无论是当我单击扩展程序中心时,还是在我想使用的扩展程序。

如果您打开扩展程序的信息 window,您可能会看到一个 Contributions 选项卡,您可能会在其中看到一个 Commands 列表。

从那里你可以找到你想要的命令并绑定到你的 keybindings.json 文件或 File -> Preferences -> Keyboard Shortcuts

[
    {
        "key": "ctrl+enter",
        "command": "command.execute",
        "when": "editorTextFocus"
    }
]

如果有人正在为 VSCode 编写自己的扩展,您可以使用 keybindings 属性以及 contributes 属性中的命令为您的命令设置默认键绑定。 package.json 中由 Yeoman yo 代码命令启动的示例项目的示例设置:

{
    "name": "static-site-hero",
    "displayName": "Static site hero",
    "description": "Helps with writing posts for static site generator",
    "version": "0.0.1",
    "engines": {
        "vscode": "^1.30.0"
    },
    "categories": [
        "Other"
    ],
    "activationEvents": [
        "onCommand:extension.helloWorld",
        "onCommand:extension.insertLink",
        "onCommand:extension.insertFigure"
    ],
    "main": "./extension.js",
    "contributes": {
        "commands": [
            {
                "command": "extension.helloWorld",
                "title": "Hello World"
            },
            {
                "command": "extension.insertLink",
                "title": "Insert Markdown Link to File or Image"
            },
            {
                "command": "extension.insertFigure",
                "title": "Insert HTML figure"
            }
        ],
        "keybindings": [
            {
                "command": "extension.insertLink",
                "key": "ctrl+alt+l",
                "mac": "shift+cmd+f"
            },
            {
                "command": "extension.insertFigure",
                "key": "ctrl+alt+F",
                "mac": "shift+cmd+l"
            }
        ]
    },
    "scripts": {
        "postinstall": "node ./node_modules/vscode/bin/install",
        "test": "node ./node_modules/vscode/bin/test"
    },
    "devDependencies": {
        "typescript": "^3.1.4",
        "vscode": "^1.1.25",
        "eslint": "^4.11.0",
        "@types/node": "^8.10.25",
        "@types/mocha": "^2.2.42"
    }
}