方法 : コマンドの既存のショートカット キーを保持する
更新 : 2007 年 11 月
通常、コマンドのショートカット キーを変更すると、既存のショートカット キーが失われます。これに対し、次の例では、2 つの新しいショートカット キーをコマンドに割り当て、そのコマンドの既存のショートカット キーも保持する方法を示します。
現在のコマンドの一覧を表示する場合は、「方法 : 既存のショートカット キーを表示する」に示されている ListKeyBindings の例を実行してください。
メモ : |
---|
使用している設定またはエディションによっては、表示されるダイアログ ボックスやメニュー コマンドがヘルプに記載されている内容と異なる場合があります。ここに記載されている手順は、全般的な開発設定が適用されているものとして記述されています。設定を変更するには、[ツール] メニューの [設定のインポートとエクスポート] をクリックします。詳細については、「Visual Studio の設定」を参照してください。 |
新しいショートカット キーを追加し、既存のショートカット キーを保持するには
Visual Studio アドイン ウィザードを使用し、新しいアドインを作成します。プロジェクトに名前を付け、[OK] をクリックしてウィザードを開始します。
Visual Studio アドイン ウィザードの使用方法の詳細については、「方法 : アドインを作成する」を参照してください。
[プログラミング言語の選択] ページで、[Visual C# を使用してアドインを作成] を選択し、後の Visual C# の例を実行するか、[Visual Basic を使用してアドインを作成] を選択し、Visual Basic の例を実行します。
後述の関数の例を、Visual Studio アドイン ウィザードによって生成されたコードの Connect クラス内に貼り付けます。
既定のキーボード設定のコピーを作成するために、C:\Program Files\Microsoft Visual Studio 8\Common7\IDE に移動します。
vsk ファイルの 1 つを右クリックし、ショートカット メニューの [コピー] をクリックします。
コピーしたファイルを同じフォルダに貼り付けます。
コピーしたファイルの名前は、"コピー ~ <vsk-file name>" になります。
コピーしたファイルの名前を変更します。
新しい .vsk ファイルがキーボードの割り当てリストに表示されることを確認するには、Visual Studio で、[ツール] メニューの [オプション] をクリックします。
[オプション] ダイアログ ボックスの左ペインで、[環境] フォルダを展開し、[キーボード] を選択します。
手順 7. で指定した .vsk ファイルの名前が [次の追加キーボード マップ スキームを適用] ボックスの一覧に表示されていることを確認します。
アドインの例を実行する前に、キーボードの割り当てが [(既定)] に設定されていることを確認します。この設定を行うには、[オプション] ダイアログ ボックスの [キーボード] ペインで [リセット] をクリックします。
アドインの例の prop.Value = "< Filename.vsk>" のステップで、<Filename.vsk> 部分を手順 7. で指定した新しいキーボード スキーム名に置き換えます。
「方法 : オートメーション オブジェクト モデルのコード例をコンパイルおよび実行する」に説明されているように、OnConnection メソッドから関数を呼び出します。
アドインをビルドします。
アドインを実行するには、[ツール] メニューの [アドイン マネージャ] をクリックし、作成したアドインを選択して、[OK] をクリックします。
File.NewFile コマンドは、元のショートカットキーに加えて、新しいショートカット キー (Ctrl + Alt + Shift + Y キーおよび Ctrl + Alt + Shift + U キー) に割り当てられます。
使用例
次のアドインの例では、既存のショートカット キーを保持したまま、新しい 2 つのショートカット キーをコマンドに割り当てる方法を示します。
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;
}