Search code examples
visual-studio-codekeyboard-shortcutskeymapping

What is the "When" and "Command" for ctrl-P panels?


I'm trying to create my own key shortcut to act as the arrow keys in Visual Studio Code.

When I hit "ctrl-p" it pops up a window that I can navigate. However, when I use my special keybinding, which I've set as

{
    "key": "alt+i",
    "command": "cursorUp",
    "when": "textInputFocus"
}

it does not move up to the next option like pressing the up arrow does.

I'm guessing this is due to having either the wrong command or the wrong "when" for getting the keymap to trigger when that window is open.

Normally in Atom I'd just use the key-binding-resolver, but Visual Studio Code doesn't appear to have that yet.

What is the correct command and "when" to navigate through the panels such as the "ctrl-p" panel in Visual Studio Code?


Solution

  • When you click Ctrl-P you are in the file picker, so you need a command that then moves up and down the filepicker. It took a bit because I thought just searching for the UpArrow bindings would make it obvious. But it did not.

    Looking again at what Ctrl-P is bound to revealed another command also bound to Ctrl-P : workbench.action.quickOpenNavigateNextInFilePicker so try:

      {
        "key": "alt+i",
        "command": "workbench.action.quickOpenNavigateNextInFilePicker",
        "when": "inFilesPicker && inQuickOpen"
      },
      {
        "key": "ctrl+p",
        "command": "-workbench.action.quickOpenNavigateNextInFilePicker",
        "when": "inFilesPicker && inQuickOpen"
      },
      {
        "key": "alt+j",
        "command": "workbench.action.quickOpenNavigatePreviousInFilePicker",
        "when": "inFilesPicker && inQuickOpen"
      },
      {
        "key": "ctrl+shift+p",
        "command": "-workbench.action.quickOpenNavigatePreviousInFilePicker",
        "when": "inFilesPicker && inQuickOpen"
      }
    

    Now choosing Alt-I and Alt-J just happens to avoid the usual Alt key trigger of the menu bar opening items, like Alt-D will open the Debug menu. If you have a conflicting Alt-Something that you wanted to use, you can disable the menu bar Alt behavior with:

    // If enabled, the main menus can be opened via Alt-key shortcuts. Disabling mnemonics allows to bind these Alt-key shortcuts to editor commands instead.

    "window.enableMenuBarMnemonics": false