问题
Question
How can a Visual Studio Code extension add key bindings to a workspace directly?
Constraints
All settings should be contained within the extension, so that any modifications by the extension are easy to find in 1 place, and uninstalling the extension will remove any modifications.
Scenario
I'm developing a VSCode extension which will need to listen for keystrokes. 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 achieve what I want using key bindings, but the implementation is not scalable and not simply exportable for others to implement.
Therefore I want to write an extension that totally encapsulates any key binding additions that are necessary.
I did find the answer to this after a long night of research, so I just want to post the solution for others' benefit.
回答1:
https://code.visualstudio.com/api/references/contribution-points#contributes.keybindings
This allows new keybindings to be declared right in the extension's package.json
来源:https://stackoverflow.com/questions/46615616/how-can-a-visual-studio-code-extension-directly-add-key-bindings