【问题标题】:How do I set a keybinding for an extension in VSCode?如何在 VSCode 中为扩展设置键绑定?
【发布时间】:2017-06-29 02:50:23
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: visual-studio-code key-bindings vscode-settings


    【解决方案1】:

    如果有人正在为 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"
        }
    }
    

    【讨论】:

    • 我已经添加了这个,但我似乎仍然必须按照其他答案手动设置键绑定才能让我的键绑定显示在开发模式中。在开发模式下运行时,有什么方法可以从 package.json 中提取键绑定?
    • 我已经在 vscode github github.com/microsoft/vscode/issues/87965987654321@ 中询问过这个问题
    【解决方案2】:

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

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

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

    【讨论】:

    • 啊,是的!我看的时候没看到。比我在下面的发现要好得多。谢谢@Get Off My Lawn(以及很棒的用户名 BTW)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 1970-01-01
    • 2021-11-02
    • 2016-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多