创建键盘快捷方式速查表。

大部分开发人员都不了解这一点,但实际上,Visual Studio 默认提供了 450 多个键盘快捷方式。不过,对于如何找到 Visual Studio 内部的所有键盘快捷方式还没有简便的方法。您可以编写一个简单的宏,对于所有默认键盘快捷方式进行遍历,找到它们所对应的操作。以下内容(列表 1)列出了这个宏的代码。

Public Module Module1

Public Sub ListShortcutsInHTML()

'Declare a StreamWriter
Dim sw As System.IO.StreamWriter
sw = New System.IO.StreamWriter("c:\\Shortcuts.html", True, System.Text.Encoding.GetEncoding("SHIFT-JIS"))

'Write the beginning HTML
WriteHTMLStart(sw)

' Add a row for each keyboard shortcut
For Each c As EnvDTE.Command In DTE.Commands
If c.Name <> "" Then
Dim bindings As System.Array
bindings = CType(c.Bindings, System.Array)
For i As Integer = 0 To bindings.Length - 1
sw.WriteLine("<tr>")
sw.WriteLine("<td>" + c.Name + "</td>")
sw.WriteLine("<td>" + bindings(i) + "</td>")
sw.WriteLine("</tr>")
Next

End If
Next

'Write the end HTML
WriteHTMLEnd(sw)

'Flush and close the stream
sw.Flush()
sw.Close()
End Sub
Public Sub WriteHTMLStart(ByVal sw As System.IO.StreamWriter)
sw.WriteLine("<html>")
sw.WriteLine("<head>")
sw.WriteLine("<title>")

sw.WriteLine("Visual Studio Keyboard Shortcuts")
sw.WriteLine("</title>")
sw.WriteLine("</head>")

sw.WriteLine("<body>")
sw.WriteLine("<h1>Visual Studio 2005 Keyboard Shortcuts</h1>")
sw.WriteLine("<font size=""2"" face=""Verdana"">")
sw.WriteLine("<table border=""1"">")
sw.WriteLine("<tr BGCOLOR=""#018FFF""><td align=""center""><b>Command</b></td><td align=""center""><b>Shortcut</b></td></tr>")

End Sub

Public Sub WriteHTMLEnd(ByVal sw As System.IO.StreamWriter)
sw.WriteLine("</table>")
sw.WriteLine("</font>")
sw.WriteLine("</body>")
sw.WriteLine("</html>")
End Sub

End Module

列表 1. 在 HTML 文件中生成键盘快捷方式的宏

要使用这个宏,请转到“工具”,选择“宏”,然后选择“宏 IDE. . .”启动“宏 IDE”。展开 MyMacros 工程,MyMacros 命名空间,然后双击“Module1”。将列表 1 中的内容复制到“宏 IDE”然后运行宏即可。运行宏之后,将会生成 Visual Studio 的键盘快捷方式参考信息。打开包含输出内容的 C:\demo\Shortcuts.html 文件。“图 1”显示了部分输出内容。如果方便就将它打印出来,贴在计算机附近,以便学习新的键盘快捷方式。