Keybinding with more than one Key programmatically [duplicate] - c#

I have a KeyBinding with the Key set to D1 which is the 1 key. This isn't the same as NumPad1 key.
Is there a way to have something like:
Key="D1 && NumPad1"
So that pressing either D1 or NumPad1 would execute the command?
I've added a second KeyBinding one for each key D1 & NumPad1, but that seems like there should be a better way.

Yes you can - comma delimited :-)
I am not certain if in the question you want to use two KeyBindings to signify that user would have to push the key twice. But that's what I was looking for. If that's the reason then this post will work.
For example, I wanted to use + to go forward, and ++ to double-go-forward, and - to go-back, -- to go double back in my app:
<KeyBinding Key="OemMinus" Modifiers="Control" Command="{Binding GoBack}"/>
<KeyBinding Key="OemMinus,OemMinus" Modifiers="Control" Command="{Binding GoBack2X}"/>
The reason I figured it out, was that I know that in VisualStudio you have a ton of commands that have double key, like commenting, or collapsing regions. And you know that VS2010's written in WPF. So I looked on the VS menu, and there are ton of commands comma separated:
View > Solution Navigator Cntr + W, F.
I tried it and it worked!

No, you can't do that.

Related

Standard keyboard shortcuts in WPF controls

I'm interested in a list of all available shortcuts of WPF controls. I was mainly interested in the standard shortcuts for WPF TreeView control (e.g., expand/collapse all, select all and so on), but I can't seem to find any location that lists them. Is there a specific page listing available shortcuts for WPF controls?
I know some shortcuts are supported naively, e.g., Ctrl + A will select all rows in a given ListView control.
I could define my own shortcuts and implement their behaviour. However, I feel it is not a good practice to define shortcuts that are already supported by the .NET framework and hence the need to know about such supported shortcuts.
In other situations I would typically use the same shortcuts available in Visual Studio as it is a WPF application, but I am hopping here for a more extensive list of out of the box supported shortcuts in WPF controls.
Thanks to psoshmo for pointing to the MSDN article. It'd be nice if they were all listed out so you didn't have to comb through a bunch of links to figure it out. So here they are. This is not all the commands, just the once with default key bindings.
Application Commands: ApplicationCommands
ContextMenu: Shift+F10
Copy: Ctrl+C || Ctrl+Insert
Cut: Ctrl+X || Shift+Delete
Delete: Del
Find: Ctrl+F
Help: F1
New: Ctrl+N
Open: Ctrl+O
Paste: Ctrl+V || Shift+Insert
Print: Ctrl+P
PrintPreview: Ctrl+F2
Properties: F4
Redo: Ctrl+Y
Replace: Ctrl+H
Save: Ctrl+S
SelectAll: Ctrl+A
Stop: Esc
Undo: Ctrl-Z
Navigation Commands: NavigationCommands
BrowseBack: Alt+Left
BrowseForward: Alt+Right
BrowseHome: Alt+Home
BrowseStop: Alt+Esc
Favorites: Ctrl+I
Refresh: F5
Search: F3
Media Commands: MediaCommands
None
Component Commands: ComponentCommands
ExtendSelectionDown: Shift+Down
ExtendSelectionLeft: Shift+Left
ExtendSelectionRight: Shift+Right
ExtendSelectionUp: Shift+Up
MoveDown: Down
MoveFocusBack: Ctrl+Left
MoveFocusDown: Ctrl+Down
MoveFocusForward: Ctrl+Right
MoveFocusPageDown: Ctrl+PageDown
MoveFocusPageUp: Ctrl+PageUp
MoveFocusUp: Ctrl+Up
MoveLeft: Left
MoveRight: Right
MoveToEnd: End
MoveToHome: Home
MoveToPageDown: PageDown
MoveToPageUp: PageUp
MoveUp: Up
ScrollPageDown: PageDown
ScrollPageUp: PageUp
SelectToEnd: Shift+End
SelectToHome: Shift+Home
SelectToPageDown: Shift+PageDown
SelectToPageUp: Shift+PageUp
Editing Commands: EditingCommands
AlignCenter: Ctrl+E
AlignJustify: Ctrl+J
AlignLeft: Ctrl+L
AlignRight: Ctrl+R
Backspace: Backspace
DecreaseFontSize: Ctrl+OemOpenBrackets
DecreaseIndentation: Ctrl+Shift+T
Delete: Delete
DeleteNextWord: Ctrl+Delete
DeletePreviousWord: Ctrl+Backspace
EnterLineBreak: Shift+Enter
EnterParagraphBreak: Enter
IncreaseFontSize: Ctrl+OemCloseBrackets
IncreaseIndentation: Ctrl+T
MoveDownByLine: Down
MoveDownByPage: PageDown
MoveDownByParagraph: Ctrl+Down
MoveLeftByCharacter: Left
MoveLeftByWord: Ctrl+Left
MoveRightByCharacter: Right
MoveRightByWord: Ctrl+Right
MoveToDocumentEnd: Ctrl+End
MoveToDocumentStart: Ctrl+Home
MoveToLineEnd: End
MoveToLineStart: Home
MoveUpByLine: Up
MoveUpByPage: PageUp
MoveUpByParagraph: Ctrl+Up
SelectDownByLine: Shift+Down
SelectDownByPage: Shift+PageDown
SelectDownByParagraph: Ctrl+Shift+Down
SelectLeftByCharacter: Shift+Left
SelectLeftByWord: Ctrl+Shift+Left
SelectRightByCharacter: Shift+Right
SelectRightByWord: Ctrl+Shift+Right
SelectToDocumentEnd: Ctrl+Shift+End
SelectToDocumentStart: Ctrl+Shift+Home
SelectToLineEnd: Shift+End
SelectToLineStart: Shift+Home
SelectUpByLine: Shift+Up
SelectUpByPage: Shift+PageUp
SelectUpByParagraph: Ctrl+Shift+Up
TabBackward: Shift+Tab
TabForward: Tab
ToggleBold: Ctrl+B
ToggleBullets: Ctrl+Shift+L
ToggleInsert: Insert
ToggleItalic: Ctrl+I
ToggleNumbering: Ctrl+Shift+N
ToggleSubscript: Ctrl+OemPlus
ToggleSuperscript: Ctrl+Shift+OemPlus
ToggleUnderline: Ctrl+U
Update 2016-01-12: I missed the Editing Commands as they are referenced in another place. The article for them also lists out the default key bindings, which can be found here.
Update 2018-04-16: Added Editing Commands and added links.
As far as I know, there is no master list of supported shortcuts, and you are right that they in general will inherit their shortcuts from the basic shortcuts in Windows. I have searched for this on my own before and have never been able to locate a list. Others I have seen asking on the internet have also never had any luck.
So the unfortunate answer is: No, there is no list.
EDIT: OK, after some digging, the best I can give you is a list of pre-defined commands that you can apply to your controls that are built into WPF. These come in five categories:
Application Commands
Navigation Commands
Component Commands
Media Commands
Editing Commands
Their documentation can be found on MSDN here (an example). Clicking on one of the commands will take you to its individual page which will list their default shortcut like this as its "Key Gesture" property.
You can assign these default commands to controls like so:
<Button Command="ApplicationCommands.Cut" CommandTarget="{Binding ElementName=txtEditor}" Width="60">_Cut</Button>
<Button Command="ApplicationCommands.Paste" CommandTarget="{Binding ElementName=txtEditor}" Width="60" Margin="3,0">_Paste</Button>
This is a seemingly pretty basic tutorial on binding these commands to buttons and what not (where that code is from). This doesn't tell you what commands are there by default, but it should be able to at least deduce the keyboard shortcuts for some of these commands, for whatever that's worth.

LeftAlt Keybinding in WPF

I'm trying to bind Left ALT key with a command to toggle visibility of a menu in WPF.
But it doesn't work.. Command is not firing..
<Window.InputBindings>
<KeyBinding
Key="LeftAlt"
Command="{Binding Path=MenuVisibilitySetCommand}"/>
</Window.InputBindings>
I've noticed that other special Keys ( such as Alt, Ctrl etc..) also not working here..
How to do KeyBinding for Special Key in WPF ?
For LeftALt to work like this, you also need to set Modifiers property to Alt.
<KeyBinding Key="LeftAlt" Modifiers="Alt" Command="{Binding Path=MenuVisibilitySetCommand}"/>
These special Keys are called Modifier keys and this should make it clear why it is not working. A modifier Key is to "modify" the behavior of a given key, Like Shift + L makes an uppercase "L" where only the L key makes a lowercase "l". Using Modifierkeys for actual logic can be problematic and irritating, because the user is not accustomed to see real actions happening when pressing these kind of buttons. But i agree there are places where this makes sense e.g. highlighting MenuItems when hitting ALT key.
But to your actual problem: You could use codebehind and the OnKeyDown/OnKeyUp or the Preview events to implement this behavior.
protected override void OnKeyDown(KeyEventArgs e)
{
if(e.SystemKey == Key.LeftAlt)
{
myMenu.Visibility = Visibility.Visible;
// e.Handled = true; You need to evaluate if you really want to mark this key as handled!
}
base.OnKeyDown(e);
}
Of course cou could also fire your command in this code.

Visual Studio: How to show Overloads in IntelliSense?

Once code has been written, the only way I know of to view the overloads for a method is to actually edit the method by deleting the Parenthesis () and reopening them.
Is there a shortcut key that I could press to activate this instead of having to edit my files?
For an example, please reference the ShowDialog Overload screen shot below:
With your cursor inside the parentheses, use the keyboard shortcut Ctrl-Shift-Space. If you changed the default, this corresponds to Edit.ParameterInfo.
Example:
Ctrl+Shift+Space shows the Edit.ParameterInfo for the selected method, and by selected method I mean the caret must be within the method parentheses.
Here is the Visual Studio 2010 Keybinding Poster.
And for those still using 2008.
Tested only on Visual Studio 2010.
Place your cursor within the (), press Ctrl+K, then P.
Now navigate by pressing the ↑ / ↓ arrow keys.
The default key binding for this is Ctrl+Shift+Space.
The underlying Visual Studio command is Edit.ParameterInfo.
If the standard keybinding doesn't work for you (possible in some profiles) then you can change it via the keyboard options page
Tools -> Options
Keyboard
Type in Edit.ParameterInfo
Change the shortcut key
Hit Assign
It happens that none of the above methods work. Key binding is proper, but tool tip simply doesn't show in any case, neither as completion help or on demand.
To fix it just go to Tools\Text Editor\C# (or all languages) and check the 'Parameter Information'. Now it should work
Great question; I had the same issue. Turns out that there is indeed a keyboard shortcut to bring up this list: Ctrl+Shift+Space (a variation of the basic IntelliSense shortcut of Ctrl+Space).
The command Edit.ParameterInfo (mapped to Ctrl+Shift+Space by default) will show the overload tooltip if it's invoked when the cursor is inside the parameter brackets of a method call.
The command Edit.QuickInfo (mapped to Ctrl+KCtrl+I by default) will show the tooltip that you'd see if you moused over the cursor location.
I know this is an old post, but for the newbies like myself who still hit this page this might be useful.
when you hover on a method you get a non clickable info-box whereas if you just write a comma in the method parenthesis the IntelliSense will offer you the beloved info-box with the clickable arrows.
Every once and a while the suggestions above stop working, if I restart Visual Studio they start working again though.
you mean's change overload.
just Press Shift + ↑ / ↓
Mine showed up in VS2010 after writing the first parenthesis..
so, prams.Add(
After doings something like that, the box with the up and down arrows appeared.

How Do I Emulate a Keystroke In C# WPF Application?

Inside the application there is an if statement. It checks a string variable kai if the variable equals to 1 i want it to react as if i pressed the right arrow on my keyboard. Can you help me with that? Thanks.
Actually you might want to think about using Commands for that. If you think about what you want to achieve it is to trigger something after the Key-Press. If you use a Command for that you can directly trigger it without going via the programmatic key pressing stuff. Also you can still trigger the command via a keypress (InputGesture).
You can find out more about Commands here: http://msdn.microsoft.com/en-us/library/system.windows.input.icommand.aspx
maybe like this:
SendKey.Send({rigth});
set focus to the control you want to input before.
And you ave to declare
Using System.Windows.Form

KeyBinding with no modifiers (in UserControl)

I want to execute a command when a key pressed(without modifiers). So, I tried below code:
<UserControl.InputBindings>
<KeyBinding Key="A" Command="{Binding ACommand}" />
</UserControl.InputBindings>
but, KeyBinding supports not a key but key and modifier.
Now I consider using Behavior and treat KeyDown event. But this is too complex.
Someone knows an easier solution?
Add:
Above code is work well in Window.
I find solution in UserControl. (Using .NET Framework 4)
I do not quite get what your problem is. The code you wrote should work, there is no need to specify modifiers.
I suppose the problem might be your binding or the control you set the key binding on cannot be focused so it won't trigger.
Microsoft doesn't support binding to a normal key that you might press while typing in a text box - unless it has a modifier. You can bind to function keys (like F1) and a few others without a modifier.
See: http://msdn.microsoft.com/en-us/library/system.windows.input.keygesture(v=vs.110).aspx
I've come across a blog that says he was able to get around this problem by creating his own KeyGesture class by inheriting from InputGesture and implementing the Matches function. http://dikhi.wordpress.com/2008/06/27/keygesture-doesnt-work-with-alpha-numeric-key/
I figured a way out to set a InputBinding by using only a single Key like "Key.A"
boolean noMod = ModifierKeys == ModifierKeys.None;
KeyBinding inputBinding = new KeyBinding(this, Keys, noMod ? ModifierKeys.Alt : ModifierKeys));
if (noMod)
{
inputBinding.ClearValue(KeyBinding.ModifiersProperty);
}
That worked for me fine.

Categories

Resources