VSCode extension API for keybindings

Viewed 1232

Trying to dynamically create keybindings from within an extension. I can't seem to find any API that supports this.

Anyone knows?

3 Answers

This link should show you how to do it. https://code.visualstudio.com/api/references/contribution-points#contributes.keybindings

Steps:

  1. register a new command (in extenson.ts)

    let disposable = vscode.commands.registerCommand('extension.helloWorld', () => {
      console.log("command ran");
    });
    context.subscriptions.push(disposable);`
    
  2. Then add it to the activation events (in package.json)

    "activationEvents": [
      "onCommand:extension.helloWorld",
      "onCommand:extension.newComment"
    ],
    
  3. 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.

Related