Adding Keyboard Shortcuts
The KEYBINDINGS_SECTION – KEYBINDINGS_END section of the .ctc file allows you to map a keyboard binding to your editor command.
Note
Beginning with Visual Studio 2008 SDK, use XML Command Table (.vsct) files instead of command table configuration (.ctc) files to define how menus and commands appear in your VSPackages. For more information, see Visual Studio Command Table (.Vsct) Files.
Use this document only for editing legacy VSPackages that use .ctc files. For adding keyboard shortcuts to packages that use .vsct files, see How to: Bind Keyboard Shortcuts to Menu Items.
Adding a Keyboard Binding to the .ctc File
The KEYBINDINGS_SECTION - KEYBINDINGS_END section of the .ctc file determines the keyboard mapping of the commands. Commands can have up to two keyboard bindings associated with them. This is useful for having multiple key bindings for a single command. A Cut command, for example, could have both "SHIFT+DELETE" and "CTRL+X" as key bindings.
The KEYBINDINGS_SECTION is comprised of 4 parameters:
Parameter |
Description |
---|---|
GUID:CmdId |
The ID of the command you want to assign a key binding to. |
When available (GUID) |
The GUID of the editor in which the key binding should be available. |
Emulation (GUID) |
The GUID of the emulation in which the key binding is available. |
Keystate |
The key(s) to bind to. Has the format: Key:Accel : Key:Accel. The second Key:Accel is used if you want to define a second keyboard binding. The Key refers to the actual key in single quotation marks, such as 'A'; or it could be a virtual key code, such as VK_F1. The Accel refers to a combination of the following letters: A = ALT, S = SHIFT, C = CTRL, W = Windows logo key |
Sample Implementation
The following sample is from a PkgCmd.ctc file.
KEYBINDINGS_SECTION
// Command when available emulation keystate
guidTestEditorCmdSet:cmdidColorFont,
guidTestEditorCmdUI,
guidTestEditorCmdUI,
'G':CSA;
KEYBINDINGS_END
The above code adds a "CTRL+SHIFT+ALT+G" keyboard binding to the Color Text command — defined by the command ID cmdidColorFont — and binds it to the editor defined by the guidTestEditorCmdUI GUID. This GUID is defined by the pguidCmdUI parameter in the CreateEditorInstance method in the EditorFactory.cs file of the walkthrough, as shown below:
Public Function CreateEditorInstance(_
ByVal grfCreateDoc As UInteger, _
ByVal pszMkDocument As String, _
ByVal pszPhysicalView As String, _
ByVal pvHier As IVsHierarchy, _
ByVal itemid As UInteger, _
ByVal punkDocDataExisting As System.IntPtr, _
ByRef ppunkDocView As System.IntPtr, _
ByRef ppunkDocData As System.IntPtr, _
ByRef pbstrEditorCaption As String, _
ByRef pguidCmdUI As Guid, _
ByRef pgrfCDW As Integer) As Integer
Trace.WriteLine(String.Format(CultureInfo.CurrentCulture, "Entering {0} CreateEditorInstance()", Me.ToString()))
' Initialize to null
ppunkDocView = New System.IntPtr()
ppunkDocData = New System.IntPtr()
'Assign guidTestEditorCmdUI to pguidCmdUI
pguidCmdUI = Vsip.TestEditor.GuidList.guidTestEditorCmdUI
pgrfCDW = 0
pbstrEditorCaption = Nothing
' Validate inputs
If (grfCreateDoc And (VSConstants.CEF_OPENFILE Or VSConstants.CEF_SILENT)) = 0 Then
Debug.Assert(False, "Only Open or Silent is valid")
Return VSConstants.E_INVALIDARG
End If
If punkDocDataExisting <> IntPtr.Zero Then
Return VSConstants.VS_E_INCOMPATIBLEDOCDATA
End If
' Create the Document (editor)
Dim newEditor As EditorPane = New EditorPane(myPackage)
ppunkDocView = Marshal.GetIUnknownForObject(newEditor)
ppunkDocData = Marshal.GetIUnknownForObject(newEditor)
pbstrEditorCaption = ""
Return VSConstants.S_OK
End Function
public int CreateEditorInstance(
uint grfCreateDoc,
string pszMkDocument,
string pszPhysicalView,
IVsHierarchy pvHier,
uint itemid,
System.IntPtr punkDocDataExisting,
out System.IntPtr ppunkDocView,
out System.IntPtr ppunkDocData,
out string pbstrEditorCaption,
out Guid pguidCmdUI,
out int pgrfCDW)
{
Trace.WriteLine(string.Format(CultureInfo.CurrentCulture,
"Entering {0} CreateEditorInstance()", this.ToString()));
// Initialize to null
ppunkDocView = new System.IntPtr ();
ppunkDocData = new System.IntPtr ();
//Assign guidTestEditorCmdUI to pguidCmdUI
pguidCmdUI = Vsip.TestEditor.GuidList.guidTestEditorCmdUI;
pgrfCDW = 0;
pbstrEditorCaption = null;
/ Validate inputs
if ((grfCreateDoc & (VSConstants.CEF_OPENFILE |
VSConstants.CEF_SILENT)) == 0)
{
Debug.Assert(false, "Only Open or Silent is valid");
return VSConstants.E_INVALIDARG;
}
if (punkDocDataExisting != IntPtr.Zero)
{
return VSConstants.VS_E_INCOMPATIBLEDOCDATA;
}
// Create the Document (editor)
EditorPane newEditor = new EditorPane(myPackage);
ppunkDocView = Marshal.GetIUnknownForObject(newEditor);
ppunkDocData = Marshal.GetIUnknownForObject(newEditor);
pbstrEditorCaption = "";
return VSConstants.S_OK;
}
The guidTestEditorCmdUI GUID is defined in Guids.cs file of the walkthrough.
See Also
Concepts
KEYBINDINGS_SECTION – KEYBINDINGS_END