方法 : コマンドを複数のショートカット キーに割り当てる
更新 : 2007 年 11 月
1 つのコマンドに対し、単一のショートカット キーではなく、複数のショートカット キーを割り当てることができます。複数のショートカット キーを使用すると、たとえば、1 つのプロジェクトで作業を行う 2 人のユーザーが、同じコマンドの実行に異なるショートカット キーを使う場合に便利です。複数のショートカット キーを割り当てるには、ショートカット キーを Object 型の配列内の文字列要素として渡します。
メモ : |
---|
使用している設定またはエディションによっては、表示されるダイアログ ボックスやメニュー コマンドがヘルプに記載されている内容と異なる場合があります。ここに記載されている手順は、全般的な開発設定が適用されているものとして記述されています。設定を変更するには、[ツール] メニューの [設定のインポートとエクスポート] をクリックします。詳細については、「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] をクリックします。
コマンドが 2 つの異なるショートカット キーに割り当てられます。Ctrl + Shift + Alt + Y キーまたは Ctrl + Shift + Alt + X キーを押すと、[新しいファイル] ダイアログ ボックスが表示されます。
使用例
次の例では、既存のキー割り当てを 2 つの新しいキー割り当てで置き換えます。
Public Sub OnConnection(ByVal application As Object, ByVal _
connectMode As ext_ConnectMode, ByVal addInInst As Object, _
ByRef custom As Array) Implements IDTExtensibility2.OnConnection
_applicationObject = CType(application, DTE2)
_addInInstance = CType(addInInst, AddIn)
BindSingle(_applicationObject)
End Sub
Sub BindSingle(ByVal dte As DTE2)
' Adds two new keybindings to a command.
Dim cmds As Commands
Dim cmd As Command
Dim props As EnvDTE.Properties = DTE.Properties("Environment", _"Keyboard")
Dim prop As EnvDTE.Property
Dim bindings(1) As Object
' Make a writeable copy of the default keymapping scheme.
prop = props.Item("SchemeName")
prop.Value = "<FileName.vsk>"
' Assign the two shortcut key combinations, CTRL+SHIFT+ALT+Y and
' CTRL+SHIFT+ALT+X, to the two bindings array elements.
bindings(0) = "Global:: CTRL+SHIFT+ALT+Y"
bindings(1) = "Global:: CTRL+SHIFT+ALT+X"
' Set references to the Commands collection and the File.NewFile
' command.
cmds = DTE.Commands
cmd = cmds.Item("File.NewFile")
' Assign the contents of the bindings array to the Bindings
' property.
cmd.Bindings = bindings
End Sub
public void OnConnection(object application,
ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
// Pass the applicationObject member variable to the code example.
BindMultiple(_applicationObject );
}
public void BindMultiple( DTE2 dte )
{
// Adds two new keybindings to a command.
Commands cmds = null;
Command cmd = null;
EnvDTE.Properties props = dte.get_Properties( "Environment",
"Keyboard");
EnvDTE.Property prop = null;
Object[] bindings = new Object[ 2 ];
// Make a writeable copy of the default keymapping scheme.
prop = props.Item( "SchemeName" );
prop.Value = "<FileName.vsk>";
// Assign the two shortcut key combinations, CTRL+SHIFT+ALT+Y and
// CTRL+SHIFT+ALT+X, to the two bindings array elements.
bindings[ 0 ] = "Global:: CTRL+SHIFT+ALT+Y";
bindings[ 1 ] = "Global:: CTRL+SHIFT+ALT+X";
// Set references to the Commands collection and the File.NewFile
// command.
cmds = dte.Commands;
cmd = cmds.Item( "File.NewFile", -1 );
// Assign the contents of the bindings array to the Bindings
// property.
cmd.Bindings = bindings;
}