'How can I find what command a keybinding is bound too easily?
In VSCode: I would like to press a combination of keys and see what command it is bound too, like "C-h k" in Emacs.
Solution 1:[1]
VS Code has a Couple Built-in Tools for Acquiring Key-binding info
So there is a tool that provides all the information you could ever need about a keyboard shortcut, and it provides it to you in real-time. Its called the Keyboard Shortcut Troubleshooter. Its a somewhat advanced tool, as it is text based. It not only provided the command that a Keybinding executes, but it provides the when clause as well. It also provides the source (user, bult-in, extension, etc...).
This is a rather large topic, that is quite under-documented IMO, which is probably why you didn't find anything about it, or find out that it does what you were looking for.
Because I already authored the answer below I can tell you it took me 2 hours to write this answer so hopefully it helps. Its a great tool for someone who aspires to write VS Code extensions, as its very help with the information it provides.
V.S. Code's Built-in Keyboard Shortcut Troubleshooter
NOTE: The trouble shooter is a text based logging tool, so it may be a bit difficult for some to interpret at first, especially if your a person who uses a Graphical desktop that doesn't promote CmdL use, like MacOS or Windows.
The troubleshooter prints all the information anyone could ever possibly need about a keybinding that was used, furthermore; it offers the luxurious capability of printing the "Keyboard Shortcut's" information in real-time, concurrent with user pressing the keys. On one hand it is a little helpful tool, on the other hand its a little powerhouse that demonstrates the quality of the team, & the opensource community members, who write V.S. Code.
USING THE KEYBOARD SHORTCUT TROUBLESHOOTER
press F1 to drop open the quick menu; once open, type the following:
** "Keyboard Shortcuts Troubleshooting"**
As you type, look at the different options that appear & disappear. You shouldn't have to type but a few characters before you see the option that you want, which is shown below:
** "Developer: Toggle Keyboard Shortcuts Troubleshooting"**
After you select the option above — assuming you followed the instructions correctly — the troubleshooter should now be toggled to 'ON'.
VIEWING THE TROUBLESHOOTER'S LOGS
You toggled the "Keyboard-shortcuts Troubleshooter" on, but to see its logs you need to open the panel that your terminal lives in, but instead of term
to use the troubleshooter, navigate to the OUTPUT panel, and select Log (window) from the OUTPUT panel's drop-down menu.
The Image below should help explain things if you can't get it working.
In the image you can see that I have the F1 quick menu open that shows the correct option for toggling the troubleshooter to on. I also have already opened the OUTPUT window and logged to it, so you can see what using the troubleshooter should look like.
[![Troubleshoot, Fix, Write, "V.S. Code Keyboard Shortcuts"][1]][1]
Reading the Troubleshooter:
In a snippet, under the keybinding icons, I added the output that the troubleshooter prints when I use the Keybinding (or keyboard shortcut) that is shown below.
CTRL + SHIFT + ~
[2022-05-09 19:01:12.397] [renderer1] [info] [KeybindingService]: \ From 2 keybinding entries, matched workbench.action.tasks.toggleProblems, when: panelFocus || terminalIsOpen, source: user.
[2022-05-09 19:01:13.146] [renderer1] [info] [KeybindingService]: / Received keydown event - modifiers: [ctrl,shift], code: Backquote, keyCode: 192, key: ~
[2022-05-09 19:01:13.146] [renderer1] [info] [KeybindingService]: | Converted keydown event - modifiers: [ctrl,shift], code: Backquote, keyCode: 86 ('`')
[2022-05-09 19:01:13.147] [renderer1] [info] [KeybindingService]: | Resolving ctrl+shift+[Backquote]
Knowing a little about the keybinding is important, because we want to make sure when we read the output that it relates to the keybinding, so we need to know about the keybinding.
The keybinding is a custom keybinding that I wrote myself. It toggles* the problem panel when either the editor, or the terminal, is in focus
Now the first part of every line looks like this:
[2022-05-09 18:55:14.623] [renderer1] [info] [KeybindingService]
But none of that was created by the Troubleshooter, its part of the Window-logger mechanism. It adds that format to the beginning of every line.
It is important to note that renderer1 means "VS Code 1". If I had two instances of VS Code open, one would be [renderer 1], the other would be [renderer 2].
[KeybindingService] is a reference to the "Keyboard Shortcut Troubleshooter".
Okay so we are going to strip all that crap from the beginning of each line, then I will parse the lines by hand so you can see what we have.
1. [LOG]: / Received keydown event -
modifiers: [ctrl,shift],
code: Backquote,
keyCode: 192,
key: ~
2. [LOG]: | Converted keydown event -
modifiers: [ctrl,shift],
code: Backquote,
keyCode: 86 ('`')
3. [LOG]: | Resolving ctrl+shift+[Backquote]
4. [LOG]: \ From 2 keybinding entries,
matched workbench.action.tasks.toggleProblems,
when: panelFocus || terminalIsOpen,
source: user.
Before we cover the lines remember that the keybinding used was:
CTRL + SHIFT + ~
There are 4 Lines, lets cover each one in order:
LINE #1
MODIFIERS: Modifiers are the keys that are held down while another one is pressed. Modifiers include the keys CTRL, SHIFT, ALT & SUPER
CODE: The code is the code of the key that is pressed. Here it says
"backquote", which is also sometimes known as "Back-tic".
The most important word is RECEIVED, which is the word line one starts with, henceforth; Line one should be interpreted as meaning:
Received Modifiers CTRL + SHIFT and Key Code '`'
LINE #2
Line two explains the conversion that happens. This is how VS Code handles all the various different Layouts, formats, and even keys that different keyboards have. The keys on every keyboard is converted to what are called virtual keys. Since Virtual Keys are standardized, converting keys to the appropriate Virtual Key ensures that all keyboards work the same. You can see what was received in line one, and what VS Code converted it too on line two.
LINE #3
Line three takes the converted input and checks if anything matches that keybinding, then it returns how many matches it found. Here we can see that I have two Keyboard Shortcuts that use this keybinding. That's because I have the keybinding do something else when the when clause changes. It even tells us what the when clause is.
THIS IS THE TICKET RIGHT HERE, ITS WHAT YOUR ASKED FOR:
At last we have the matched command
The info we get is:
Matched: workbench.action.tasks.toggleProblems,
When: panelFocus || terminalIsOpen,
So that means that the command workbench.action.tasks.toggleProblems is executed...
When the following, panelFocus || terminalIsOpen, returns true.
And, that when-clause is interpreted as meaning, "When the panel is in focus, or when the terminal is open"
Lastly it tells you that the source of the command is "USER". The source can also be "Extension", and/or "Built-in".
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 | j D3V |
