Useful Code: Keyboard shortcuts

People often wonder about how we deal with Keyboard shortcuts in Visual Studio. There are a couple of things to know and I'll throw in a neat macro too.

Profiles

When you run Visual Studio for the first time, you will get a prompt asking which default settings you wish to use. We created these defaults to accommodate developers have become accustomed to a certain IDE layout and we don't want to impose a new one. Therefore we ship VS with 7 default profiles corresponding to types of development activities: J#, C#, VB, C++, Web, Team and General (kind of a default for defaults). These profiles encode things like the window layout, options, help filters, as well as keyboard shortcuts. If you are accustomed to VC6 shortcuts, you'll probably be most comfortable in the C++ profile. On the other hand, if you're coming from the C#/VB world, you might prefer to stick with their settings. Of course, the elite among you will care nothing for these imposed "defaults" and I'll be happy to show you how you can set up your own shortcuts.

Keyboard Options

There is a single location for all your shortcut management needs, the Keyboard options dialog. It is located within the IDE options dialog (Tools\Options...). You can find it under the top-level Environment node. I like to say that it's a highly functional dialog stuck in a poor UI. Here is a picture of it (the red boxes are my creative addition).

As you can see, it looks quite dense so I have highlighted what I consider the two features of this dialog everyone should know (I leave the rest up to the reader). The first box is a substring search filter to find any command which can be assigned a shortcut, which is great when you can't remember what the shortcut for a command is. The second box is somewhat the compliment of the first as it allows you to do a reverse lookup of a shortcut. In other words, given a shortcut, it will display what command(s) it is assigned to.

View all shortcuts

Over time, some of my colleagues decided it would be nice to query Visual Studio to return the list of all assigned shortcuts (in order to print it out or test it or something…). This seemed like an interesting opportunity to flex our extensibility model and write yet another macro. First, let's define a couple structures for this purpose.

Structure VSShortcut

Public Scope AsString

Public KeyCombo AsString

SubNew(ByVal fullshortcut AsString)

Scope = fullshortcut.Split("::").GetValue(0)

KeyCombo = fullshortcut.Split("::").GetValue(2)

EndSub

EndStructure

Structure VSCommand

Public Name AsString

Public Category AsString

Public Shortcuts As List(Of VSShortcut)

SubNew(ByVal fullname AsString, ByVal keys As Array)

Name = fullname

Category = fullname.Split(".").GetValue(0)

Shortcuts = New List(Of VSShortcut)(5)

ForEach key AsStringIn keys

Shortcuts.Add(New VSShortcut(key))

Next

EndSub

EndStructure

These two structures are pretty self-explanatory, they represent a shortcut key combination and a command respectively. We can now iterate through the list of all commands and generate a list of commands with assigned shortcuts.

Sub GetAssignedCommands(ByRef CommandList As List(Of VSCommand))

Dim keys As System.Array

' iterate through each possible command in VS

ForEach item As EnvDTE.Command In DTE.Commands

If (item.Name <> Nothing) And (item.Name <> "") Then

' get the array of shortcuts for this command

keys = item.Bindings

' add command only if there is at least a shortcut assigned to it

If keys.Length > 0 Then

CommandList.Add(New VSCommand(item.Name, keys))

EndIf

EndIf

Next

EndSub

At this point, we simply need to print this out. It turns out there's a quick and dirty way of getting some output from a macro in VS. The Output window. Here is the relevant code to spit out some XML (HTML might be more appropriate for a print-out).