Trying to dynamically create keybindings from within an extension. I can't seem to find any API that supports this.
Anyone knows?
Trying to dynamically create keybindings from within an extension. I can't seem to find any API that supports this.
Anyone knows?
This link should show you how to do it. https://code.visualstudio.com/api/references/contribution-points#contributes.keybindings
Steps:
register a new command (in extenson.ts)
let disposable = vscode.commands.registerCommand('extension.helloWorld', () => {
console.log("command ran");
});
context.subscriptions.push(disposable);`
Then add it to the activation events (in package.json)
"activationEvents": [
"onCommand:extension.helloWorld",
"onCommand:extension.newComment"
],
Then declare your new command (in 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"
}]
},
Now whenever someone double presses k while holding cmd, the helloWorld command will run and "command ran" will be print to the console.
You can do that by contributing all of your keybindings and using a custom where clause. See https://code.visualstudio.com/api/references/when-clause-contexts#add-a-custom-when-clause-context