用于键绑定的VSCode扩展API

时间:2017-05-25 10:53:10

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

尝试从扩展程序中动态创建键绑定。 我似乎无法找到任何支持此功能的API。

有谁知道?

3 个答案:

答案 0 :(得分:0)

您可以为扩展使用Vscode命名空间API。您keybindings中有一个package.json部分。所以你可以在里面定义关键地图。

例如,你应该这样使用:

{ "key": "tab", "command": "tab", "when": ... },
{ "key": "tab", "command": "editor.emmet.action.expandAbbreviation", "when": ... },
{ "key": "tab", "command": "jumpToNextSnippetPlaceholder", "when": ... },
{ "key": "tab", "command": "acceptSelectedSuggestion", "when": ... },
{ "key": "ctrl+shift+k", "command": "editor.action.deleteLines", "when": "editorTextFocus" },

额外有用的链接:Windows虚拟密钥列表:

https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731

有关API和实践的详细信息,请查看以下链接:

https://code.visualstudio.com/docs/getstarted/keybindings#_customizing-shortcuts

答案 1 :(得分:0)

此链接应向您显示操作方法。 https://code.visualstudio.com/api/references/contribution-points#contributes.keybindings

步骤:

  1. 注册一个新命令(在extenson.ts中)

    let disposable = vscode.commands.registerCommand('extension.helloWorld', () => { console.log("command ran"); }); context.subscriptions.push(disposable);

  2. 然后将其添加到激活事件中(在package.json中)

    "activationEvents": [
      "onCommand:extension.helloWorld",
      "onCommand:extension.newComment"
    ],
    
  3. 然后声明您的新命令(在package.json中)

    "contributes": { "commands": [{ "command": "extension.helloWorld", "title": "Hello World" }], "keybindings": [{ "command": "extension.helloWorld", "key": "ctrl+k ctrl+k", "mac": "cmd+k cmd+k", "when": "editorTextFocus" }] },

现在,只要有人按住cmd连按两次k,helloWorld命令就会运行,并且“ command ran”将打印到控制台。

答案 2 :(得分:0)

您可以通过提供所有键绑定并使用自定义 where 子句来做到这一点。见https://code.visualstudio.com/api/references/when-clause-contexts#add-a-custom-when-clause-context