繁体   English   中英

如何在 VSCode 中为扩展设置键绑定?

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

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

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

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

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

如果有人正在为 VSCode 编写他们自己的扩展,您可以使用 keybindings 属性以及内部的命令为您的命令设置默认键绑定。 由 Yeoman yo 代码命令启动的示例项目的 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"
    }
}

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

在此处输入图片说明

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

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

暂无
暂无

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

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