'VS Code - find panel/view context name for shortcut 'when' expression

I'm trying to add some shortcuts to my VS Code instance and I want to scope them using the when expression to avoid conflicts.

Alas, I'm struggling to find the right context name to use in the expression to achieve what I want.

I've searched the documentation and found that a number of them are listed here: https://code.visualstudio.com/docs/getstarted/keybindings#_contexts

But, as the documentation says:

The list above isn't exhaustive and you may see some when contexts for specific VS Code UI in the Default Keyboard Shortcuts.

Of course, the context to which I want to scope my shortcuts is not on the list ;)

Thus, I was wondering if there was a way to find the context name of a specific panel/view in the VS Code UI.

To illustrate this, suppose I'd like to make a shortcut active only when the panel that lists the available NPM scripts is focused... What context name should be used to achieve that?



Solution 1:[1]

I tried this - not expecting it to work - but it seems to:

"when": "focusedView == npm"

Similar info is active/focussed view context clauses.

In general, you can:

  1. Open the developer tools: Help/Toggle Developer Tools and go to the console
  2. Choose Inspect Context Keys in the VS Code Command Palette
  3. Click on an element in the vscode window
  4. Look in the tools console and expand the last item therein to show all the current context (when clauses).

But when I did that I didn't find anything helpful and focusedView = "" was there, so that's not helpful.

so I just tried

{
  "key": "alt+i",
  "command": "workbench.action.terminal.sendSequence",
  "args": { "text": "howdy" },
  "when": "focusedView == npm"
},

that just prints howdy to the terminal for testing.. And the only way I could get it to work was when the npm scripts viewlet header or its contents had focus.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1