Visual Studio 代码扩展如何直接添加键绑定

How can a Visual Studio Code extension directly add key bindings

问题

Visual Studio 代码扩展如何直接向工作区添加键绑定?

约束条件

所有设置都应包含在扩展中,以便在一处轻松找到扩展的任何修改,卸载扩展将删除所有修改。

场景

我正在开发一个 VSCode 扩展程序,它需要监听击键。 This question listens through the vscode.workspace.onDidChangeTextDocument event, but I found that route a little messy in comparison to just creating key bindings for what I'm listening to. I found a pretty elegant way to ,但该实施不可扩展且不能简单地导出供其他人实施。

因此我想编写一个扩展,完全封装任何必要的键绑定添加。

经过一夜的研究,我确实找到了这个问题的答案,所以我只想 post 解决方案以造福他人。

https://code.visualstudio.com/api/references/contribution-points#contributes.keybindings

这允许在扩展的 package.json

中正确声明新的键绑定