Introduction
This project is an addin to SQL Server Management Studio 2005 (tested in version 9.00.3042.00 of SSMS; that's SQL 2k5 with SP1 on Vista) for managing keybindings to built in commands (like Copy and Paste), and for other addins that might be registered.
Background
I was writing some other addin, when in some weird way (must be my friend who did this) the Quick Search menu item lost its keybinding. I then wrote a very simple addin that fixed just that command, and then I thought I could improve on this addin to make a keybinding interface similar to Visual Studio 2005.
Using the code
I won't go into the details of creating an addin because there are so many others on codeproject.com that already does it. I will focus on the Command object instead.
What might be interesting is how to retrieve all the existing commands in SSMS, for example <b>Edit.Copy</b> etc. Command
objects can be found in the Commands
collection of the DTE2
object. To list all commands use:
foreach (Command command in _applicationObject.Commands) {
Debug.WriteLine(command.Name);
}
To retrieve a special command object use:
Command command = _applicationObject.Commands.Item("Edit.Copy", -1);
When you have a reference to the command object, the keybindings are in the Bindings
property. It's an object array with strings on the format of "scopename::modifiers+key".
Scopename can, for example, be Global, SQL Query Editor & View Designer. Modifiers can be Ctrl, Shift & Alt. In the screenshot above you can see that Edit.Copy is bound to Global::Ctrl+C, but it is also bound to Global::Ctrl+Ins which means that you can have several bindings for the same command for different scopes.
These keybindings are stored in a .vsk file in your Document and Settings directory (mine is found in C:\Users\johan.sassner\AppData\Roaming\Microsoft\Microsoft SQL Server\90\Tools\Shell\Current.vsk).
Installation
To use the addin you have to register it using RegAsm (found in your v2.0 framework directory), and then run the included registry file. It adds the following key to the registry:
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\90\Tools\Shell\
Addins\Sassner.SSMSKeybindings.Connect]
"LoadBehavior"=dword:00000001
"Description"="SSMSKeybindings"
"FriendlyName"="SSMS Keybindings addin,SQL Server Management Studio Extension"
Points of Interest
I did stumble upon a weird bug, which, after a long time, I found a similar reference to a bug in Visual Studio 2003. If you remove the last keybinding on a command, it is not stored in the SSMS settings file. What I had to do was to find another command and just set that command binding object again (no changes, just re-set it).
private void PersistRandomCommand() {
foreach (Command command in _applicationObject.Commands) {
if (command.Name.Length > 0) {
object[] bindings = (object[])command.Bindings;
if (bindings.Length > 0) {
command.Bindings = bindings;
break;
}
}
}
}
History
v1.0 - 2007-07-11, added first version.
v1.01 - 2007-07-11, added how to install the addin.