次の方法で共有


方法 : 既存のショートカット キーを表示する

Bindings プロパティを使用すると、指定したコマンドに関連付けられたショートカット キーを表示または変更できます。このプロパティを読み取ると、そのコマンドに対する現在のショートカット キーがオブジェクトの配列として取得されます。各オブジェクトには、ショートカット キーを示す文字列が含まれています。

Bindings プロパティに値を設定すると、指定したコマンドに 1 つ以上の新しいショートカット キーが割り当てられます。詳細については、「方法 : コマンドを単一のショートカット キーに割り当てる」および「方法: 1 つのコマンドに複数のショートカット キーを割り当てる」を参照してください。

[!メモ]

実際に画面に表示されるダイアログ ボックスとメニュー コマンドは、アクティブな設定またはエディションによっては、ヘルプの説明と異なる場合があります。ここに記載されている手順は、全般的な開発設定が適用されているものとして記述されています。設定を変更するには、[ツール] メニューの [設定のインポートとエクスポート] をクリックします。詳細については、「Visual Studio の設定」を参照してください。

既存のショートカット キーの表示

  1. Visual Studio アドイン ウィザードを使用して、新しいアドインを作成します。プロジェクトに名前を付け、[OK] をクリックしてウィザードを開始します。

    Visual Studio アドイン ウィザードの使い方の詳細については、「方法 : アドインを作成する」を参照してください。

  2. [プログラミング言語の選択] ページで、[Visual C# を使用してアドインを作成] を選択して、後で示す Visual C# の例を実行するか、[Visual Basic を使用してアドインを作成] を選択して、Visual Basic の例を実行します。

  3. 後述の関数の例を、Visual Studio アドイン ウィザードによって生成されたコードの Connect クラス内に貼り付けます。

  4. 方法 : オートメーション オブジェクト モデルのコード例をコンパイルおよび実行する」に説明されているように、OnConnection メソッドから関数を呼び出します。

  5. アドインを実行するには、[ツール] メニューの [アドイン マネージャー] をクリックし、作成したアドインを選択します。次に、[OK] をクリックします。

    File.NewFile コマンドに割り当てられたすべてのショートカット キーの一覧が表示されます。

使用例

次の例では、File.NewFile コマンドに割り当てられたすべてのショートカット キーを表示することにより、Bindings を使用する方法を示します。

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)
    ' Pass the applicationObject member variable to the code example.
    ListKeyBindings(_applicationObject)
End Sub

Sub ListKeyBindings(ByVal dte As DTE2)
    ' Bindings() is an array of key binding string names.
    Dim bindings() As Object
    Dim binding As Object
    Dim msg As String = Nothing
    ' Populate the collection with all of the bindings
    ' for the command File.NewFile.
    bindings = dte.Commands.Item("File.NewFile").Bindings
    For Each binding In bindings
        msg += CStr(binding) & vbCr
    Next
    MsgBox(msg)
 End Sub
// Add-in code.
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.
    ListKeyBindings((DTE2)_applicationObject);
}
public void ListKeyBindings(DTE2 dte)
{
    object[] bindings;
    string msg = string.Empty;
    // Populate the collection with all of the bindings associated
    // with the command File.NewFile.
    // Bindings() is an array of key binding string names.
    bindings = (object[])dte.Commands.Item("File.NewFile", 0).Bindings;
    foreach (object b in bindings)
    {
        msg += ((string)b) + "\n";
    }
    System.Windows.Forms.MessageBox.Show(msg);
}

参照

処理手順

方法: 既存のショートカット キーを保持する

概念

Bindings プロパティのパラメーター形式

その他の技術情報

アドイン コマンドのキーへの割り当て