Scorb
Scorb

Reputation: 1860

Visual Studio Code - make shortcut run only in editor focus

I am trying to make a keyboard shortcut only run when focus is currently in the text editor, and specifically not in the VSCode file explorer sidebar.

I have observed all the examples in the keybindings.json and tried to follow their pattern.

The default binding in the default keybindings is as follows...

{ "key": "ctrl+n",                "command": "workbench.action.files.newUntitledFile" },

The keyboard mapping I added in the user keybindings is as follows....

{ "key": "ctrl+n", "command": "workbench.action.files.newUntitledFile" , "when": "editorTextFocus && !filesExplorerFocus" }

But for some reason this is not working. Even when I have focus in the VSCode File Explorer menu, the command is still running.

What am I missing here?

EDIT:

I have tried unmapping the initial binding, which succeeds at removing the default binding. But when I add the new binding with the when clauses, the when clauses have no effect.

{ "key": "ctrl+n", "command": "workbench.action.files.newUntitledFile" , "when": "editorTextFocus && !filesExplorerFocus" },

{ "key": "ctrl+n", "command": "-workbench.action.files.newUntitledFile" },

Upvotes: 1

Views: 620

Answers (1)

Mark
Mark

Reputation: 181060

If you also disable the default keybinding it works as expected:

{
    "key": "ctrl+n",
    "command": "-workbench.action.files.newUntitledFile"
}

Otherwise, when you have fileExplorerFocus, this command will be in scope and active.

Upvotes: 1

Related Questions