Having a Visual Studio extension (VSIX) project: In Window
we got UserControl
, which have Button
binded to some ICommand
. This works perfectly as expected, however I would like to attach a key shortcut (e.g.:CTRL + S) which would fire the same Command
.
I have checked several questions, in which I found the most useful this code:
<UserControl.InputBindings>
<KeyBinding Modifiers="Ctrl" Key="Esc" Command="{Binding SaveCmd}"/>
</UserControl.InputBindings>
However the Command
is never fired from the key-press, I think the issue(s) might be:
- code above should not be working? (I found article where the bind should be done to the
Command
withDependencyProperty
) - The key-press is caught by Visual Studio itself (CTRL + S is saving the file)
- I might need to set the binding on the
Window
which encapsulates theUserControl
- I might need to set the binding in the
*Package.vsct
and route it through as it would be aCommand
in Visual Studio
Question(s): How am I suppose to bind to the shortcut key-press? Where am I suppose to place the binding?
You're right, the shortcut is used by a default Visual Studio command which takes precedence over the extension.
From a similar msdn post, this behavior is confirmed and the suggestion is to choose a different combination.
Find a reference for the complete list of VS shortcuts. Shortcut keys apply at various scopes (for example, when you are in a text editor, the Text Editor scoped shortcuts take precedence over the Global shortcuts). Aside from that, you can customize the shortcut's behavior and also import a new keyboard mapping scheme and select it under Tools > Options > Environment > Keyboard.
The
KeyBindings
section in .vsct is where you can associate a command with a keyboard shortcut. Microsoft sample is on githubKeyBindings
seems complicated and needs to be definied on several steps (also depends on requirements). This answer is written as a bonus to answer of user1892538.Scenario: We got toolWindow which is already showing, but we want to add some command, which will invoke method in the view/view-model.
1. Create new
Command
(Step 3 in this tutorial):Right-click to the project -> Add
New Item
->Custom command
. This will create 2 files and modify file with package:CommandName.png
- icon for the menuCommandName.cs
- class file including the source code of commandProjectWindowPackage.cs
- Package class withInitialize()
method, which invokesInitialize()
ofCommandName.cs
MyWindowPackage.cs
:CommandName.cs
:2. Set content of MyToolWindow to MyWindowControl (done when VSIX created):
MyToolWindow.cs
:3. Set code in code-behind to invoke ViewModel (or do the job itself):
MyWindowControl.cs
:4. Set
Command
withShortcut
so VS know how to handle them:In MZTools' article can be found solution how to add
Command
without seeing it in menu, but if You will go to Tools->Window->Keyboard, You might find them there (so You can set up shortcut).I will show both origin
Button
(for displaying tool window) and 2nd invisibleButton
used for the shortcut (Keybind
) only.MyWindowPackage.vsct
(in several parts):KeyBindings (shortcut definition):
And the
Symbols
, which set and bindGUID
,Command definition
andCommand logic
together:Bonus:
KeyBinding
does have propertyeditor="guidVSStd97"
, this will set the scope of binding to "GENERAL" (useable in each window). If You can set this to theGUID
of YourToolWindow
, it will be used only when theToolWindow
is selected. How it works, is described behind this link. And to acomplish it full, get to this link.Good answer from user1892538
Also, beware that some shortcuts are taken by the operating system or by other software running on the machine.
Ctrl+Esc will activate the start menu on Windows machines.
Other examples: - Intel graphics software takes over Ctrl+Alt+F8. - Some Ctrl+Alt combinations can have trouble getting through remote desktop connections.