HOW TO:保留現有的命令鍵繫結
更新:2007 年 11 月
通常當您變更命令的按鍵繫結時,就會失去現有的按鍵繫結。但是,在下列範例中,會示範如何將兩個新的組合鍵繫結至同一個命令,同時仍然保留現有的按鍵繫結。
如果您想要查看目前的命令清單,請依照 HOW TO:檢視現有的金鑰繫結主題中所提供的方式執行 ListKeyBindings 範例。
![]() |
---|
根據目前使用的設定與版本,您所看到的對話方塊與功能表命令可能會與 [說明] 中所描述的不同。使用 [一般開發設定] 開發了這些程序。若要變更設定,請從 [工具] 功能表中選擇 [匯入和匯出設定]。如需詳細資訊,請參閱 Visual Studio 設定。 |
若要加入新的快速鍵並且保留現有命令的按鍵繫結
請使用 [Visual Studio 增益集精靈] 建立新的增益集 (Add-in)。命名專案,再按 [確定] 啟動精靈。
如需使用 [Visual Studio 增益集精靈] 的詳細資訊,請參閱 HOW TO:建立增益集。
在 [選取程式語言] 頁上,選取 [使用 Visual C# 建立增益集] 以執行以下的 Visual C# 範例,或選取 [使用 Visual Basic 建立增益集] 以執行 Visual Basic 範例。
在 [Visual Studio 增益集精靈] 產生之程式碼的 Connect 類別中,貼上以下的範例函式。
若要複製預設的鍵盤設定,請巡覽至 C:\Program Files\Microsoft Visual Studio 8\Common7\IDE。
以滑鼠右鍵按一下其中一個 vsk 檔,然後從快速鍵功能表中選取 [複製]。
將複製的檔案貼入同一個資料夾中。
複製的檔案會命名為「複本 - <vsk 檔名>」。
重新命名複製的檔案。
若要確認新的 vsk 檔是否顯示在鍵盤繫結清單中,請在 Visual Studio 中,按一下 [工具] 功能表上的 [選項]。
在 [選項] 對話方塊的左窗格中,展開 [環境] 資料夾並選取 [鍵盤]。
請確認您在步驟 7 中指定的 vsk 檔案名稱,顯示在 [套用下列其他鍵盤對應配置] 下拉式功能表 (Drop-Down Menu) 中。
在執行增益集範例之前,請確定鍵盤繫結必須設為 [(預設)]。作法是在 [選項] 對話方塊中,按一下 [鍵盤] 窗格的 [重設]。
在增益集範例的 prop.Value = "< Filename.vsk>" 步驟中,以步驟 7 中所指定的新的鍵盤配置名稱取代 <Filename.vsk>。
從 OnConnection 方法呼叫函式,如 HOW TO:編譯和執行 Automation 物件模型程式碼範例所述。
建置增益集。
若要執行增益集,請按一下 [工具] 功能表上的 [增益集管理員]、選取您建立的增益集,然後按一下 [確定]。
File.NewFile 命令便會繫結至新的按鍵組合 (CTRL+ALT+SHIFT+Y 和 CTRL+ALT+SHIFT+U) 以及原來的按鍵組合。
範例
在下列增益集範例中,會示範如何將兩個新的組合鍵繫結至同一個命令,同時保留現有的按鍵繫結。
Sub PreserveBindings()
' Adds two new key bindings while preserving the existing ones.
Dim cmds As Commands
Dim cmd As Command
Dim props As EnvDTE.Properties = DTE.Properties("Environment", _
"Keyboard")
Dim prop As EnvDTE.Property
Dim bindings() As Object
Dim bindingNumber As Integer
' Set references to the Commands collection and the File.NewFile
' command.
cmds = DTE.Commands
cmd = cmds.Item("File.NewFile")
' Make a writeable copy of the default keymapping scheme.
prop = props.Item("SchemeName")
prop.Value = "<FileName.vsk>"
' Retrieve the current bindings for the command.
bindings = cmd.Bindings
' Get the number of bindings for the command.
bindingNumber = bindings.Length
' Add two more elements to the array to accomodate two
' new commands.
ReDim Preserve bindings(bindingNumber + 1)
' Add the new bindings to the existing ones in the array.
bindings(bindingNumber) = "Global::CTRL+ALT+SHIFT+Y"
bindings(bindingNumber + 1) = "Global::CTRL+ALT+SHIFT+U"
' Assign the contents of the bindings array to the Bindings
' property.
cmd.Bindings = bindings
End Sub
public void OnConnection(object application,
Extensibility.ext_ConnectMode connectMode, object addInInst, ref
System.Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
// Pass the applicationObject member variable to the code example.
PreserveBindings((_applicationObject);
}
// Add-in example for TextSelection.FindPattern.
// Also shows usage of these methods and properties:
// TextSelection.SelectLine
public void PreserveBindings( DTE dte )
{
// Adds two new key bindings while preserving the existing ones.
Commands cmds = null;
Command cmd = null;
EnvDTE.Properties props = dte.get_Properties( "Environment",
"Keyboard");
EnvDTE.Property prop = null;
Object[] bindings = null;
int bindingNumber = 0;
// Set references to the Commands collection and the File.NewFile
// command.
cmds = dte.Commands;
cmd = cmds.Item( "File.NewFile", -1 );
// Make a writeable copy of the default keymapping scheme.
prop = props.Item( "SchemeName" );
prop.Value = "<FileName.vsk>";
// Retrieve the current bindings for the command.
bindings = ( ( System.Object[] )( cmd.Bindings ) );
// Get the number of bindings for the command.
bindingNumber = bindings.Length;
// Add two more elements to the array to accomodate two
// new commands.
// Create temp variable for copying values.
// Arrays are zero-based in C#.
object[] temp = new object[ bindingNumber + 2 ];
System.Array.Copy( bindings, temp, Math.Min( bindings.Length,
temp.Length ) );
bindings = temp;
// Add the new bindings to the existing ones in the array.
bindings[ bindingNumber ] = "Global::CTRL+ALT+SHIFT+Y";
bindings[ bindingNumber+1 ] = "Global::CTRL+ALT+SHIFT+U";
// Assign the contents of the bindings array to the Bindings
// property.
cmd.Bindings = bindings;
}