11

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

我安装的扩展没有为我提供轻松调用它的键绑定。

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

似乎我必须发现扩展程序的命令,但我不知道在哪里可以找到,当我单击扩展程序集线器,然后单击我想使用的扩展程序时,在扩展程序摘要页面上似乎并不明显.

4

2 回答 2

31

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

{
    "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"
    }
}
于 2018-12-26T21:19:02.610 回答
19

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

在此处输入图像描述

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

[
    {
        "key": "ctrl+enter",
        "command": "command.execute",
        "when": "editorTextFocus"
    }
]
于 2017-02-10T17:09:07.037 回答