Standard keyboard shortcuts in WPF controls - c#

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.

Related

Why do Enter and Space keys behave differently for buttons?

As far as I know, these are the only keys that react when a button has focus.
Pressing Enter instantly 'clicks' the button, even if you keep it the key down. (So the 'click' happens on KeyDown).
Pressing Space acts more like a normal mouse click; holding it down doesn't activate the Click event, but it does once you release it. (So the 'click' happens on KeyUp or KeyPressed.)
Why the difference? I'd like a good article on the subject or simply a logical explanation as to why those two keys have different behavior. Surely there's an explanation out there!
I can't find any articles explaining this and it's a really good question. I personally think that it's for functionality purposes
Enter Key the classic AcceptButton acts like a FullClick (Click/ClickReleased) that's why if you hold it you will have the effect of clicking multiple times.
Space however is a SingleClick (No click release until you release the key) so it can accomplish task where only a Click is required without a ClickRelease and actions where only the selection of a control is required to activate it. Like the CheckBox or RadioButtons which can't be activate with the Enter but can be activated with the Space like if you click on it.
In conclusion, the Space would be the official MouseClick since it has the same effects of a MouseClick uppon pressing or releasing. Enter would be sort of a shortcut for a One click full click. All, of course, in the idea of giving more possibilities to the keyboard itself.
You're seeing two different behaviors, which aren't associated except that they both deal with keyboard events on a winform.
Enter is special because it's the keypress to activate the acceptButton of a form. In fact, you missed another key that can affect buttons: Esc is the cancelButton, and will throw events as well.
As PhaDaPhunk explained, Space is a MouseClick for any component that accepts a MouseClick, but I haven't found a detailed explanation for it. I'd assume it's the default behavior of all controls. The Microsoft guide to accessibility seems to imply that is so in their section on keyboard-based navigation
Incidentally, this Microsoft support knowledge base entry seems to show that the spacebar implementation went from Button.Click to Button.MouseClick. Perhaps that's the reason for it's different behavior.
This functionality seems to have been removed in Big Sur. I came here looking for how I could get it back. It can be very efficient to click enter to proceed or spacebar usually to cancel, to pick the two primary options on most dialog buttons.

How to associate an event with a menuitem in WPF using VS2008?

I am in the process of learning WPF (I don't know much yet). I know how to associate a menu subitem to an event by directly editing the xaml but, I'd like to know how to do the same thing using Visual Studio.
For instance, consider the following XAML snippet:
<Menu DockPanel.Dock ="Top"
HorizontalAlignment="Left" Background="White" BorderBrush ="Black">
<MenuItem Header="_File"/>
<Separator/>
<MenuItem Header ="_Exit"
MouseEnter ="MouseEnterExitArea"
MouseLeave ="MouseLeaveArea"
Click ="FileExit_Click"/>
I'd like to be able to associate the "_Exit" menuitem with the appropriate event handlers for MouseEnter, MouseLeave and Click using Visual Studio instead of manually editing the xaml text. I was trying to accomplish this by using the "Items" collection editor but, it seems to only allow editing of the subitem's properties and not the events. Is there another editor/dialog/etc that allows editing the subitem's events ?
Thank you for your help,
John.
P.S: thank you H.B for taking the time to format the xaml properly. :-)
It does not seem to be possible to do this.
I realize this is an old thread but since I came across it, I figured someone else may benefit from a method that I found to use Visual Studio to view/edit the Event Handlers definitions of sub-MenuItems.
This method still requires that you access the XAML file but you do not have to edit it. Simply select (click) the record in the XAML file which defines the sub-MenuItem and you will be able to access the Events page in the Properties Window (F4).
If Microsoft is listening, I would say this is a design flaw (bug?) in the Collection Editor.
I'm not sure what you mean by "edit with visual studio", so I hope I'm on the right track here...
You can give the items names in xaml:
<MenuItem Name="exitMenuItem" ....>
then in the c# codebehind:
exitMenuItem.Click += (s,e) =>
{
DoIt(); // your event handling code
}
on the Properties window for that item switch to the Events tab then double click on the box next to the event you want.
in 2008 I think that there is a button that looks like a lightning bolt that you can click to get the events. It should be in the same area as the buttons to sort alphabetically or to order by category or whatever.

Backspace not working for IE Toolbar

I am developing the Internet Explorer Toolbar in c#.net using the band objects.
Now in my toolbar, I am using the textbox field to make the search enable, but in this textbox field, I am not able to use the backspace, delete, arrow keys and many other such button.
I am not sure about y I am not able to use this. Please help me about this. I found many question posted over like this, but none of them was having the specific answer.
Thanks
The problem is that the browser is eating the events for those keystrokes, so the solution is to force focus to the toolbar when the text box receives focus.
To fix it add this line to your toolbar's constructor:
yourTextBox.GotFocus += (sender, args) => OnGotFocus(args);
Also make sure you have implemented TranslateAcceleratorIO() per this example.
Compare your code to this one and see what's missing.

MS-VS2008 Keyboard mapping problem

I am trying to set my own hotkey combination for the action Edit.FindNext in visualstudio 2008 under:
Tools.Customize -> Keyboard
but these simply dont take effect, so as soon as i close the dialog my VS keeps working with the default combination: Ctrl + F3, and the one i set is just ignored, although its still set in the configuration.
restart didnt help.
Any ideas?
Thank you.
I found the problem,
i tried to set the Ctrl + Left Arrow for the find next action but the Left-Arrow can not be used in global as microsoft states:
The following keys cannot be assigned
to a command in Global: PRINT SCRN/SYS
RQ, SCROLL LOCK, PAUSE/BREAK, TAB,
CAPS LOCK, INSERT, HOME, END, PAGE UP,
PAGE DOWN, Windows logo keys,
Application key, any of the ARROW
keys, or ENTER; NUM LOCK, DEL, or
CLEAR on the numeric keypad; or
CTRL+ALT+DELETE.
so i had to set this to the texteditor context, and it worked.
Setting a hotkey in VS can be tricky. Be aware of the following: The same hotkey can be used for different things, depending on the context.
For example, it is possible, that you defined the hotkey for "Global", but for "Text Editor" your default hotkey is still registered and the new one might even be registered for something else in the context of "Text Editor". So when you enter your new hotkey, check out the Combobox "Shortcut currently used by", maybe it will show you what the problem is.

Is there a way to press or fireup the keys in key board using c# code?

I want the 'Alt' to be pressed by code. It is like firing key-press event with key 'Alt' by the code, not hitting it manually. The need is, I have set 'ShortCut keys for menu, but it (the single underline on key letter) is not visible to the user unless he presses 'alt'. So i need to make the Alt be pressed by default.
Is there a way to 'press' or 'fireup' the keys in key board using c# code?
Check out the System.Windows.Forms.SendKeys class.
You can use the static Send method to send keystrokes to the active window. If you're trying to send keystrokes to another window, you'll need to use the Windows API to activate the other window first.
If you have any control over the operating system on which the program is being deployed, apparently you can force the underlined shortcut letter to always be displayed by going to Control Panel -> Display -> Appearance -> Effects -> Hide underlined letters for keyboard navigation.
(http://www.chinhdo.com/20080902/underlined-letters-windows/)
here is a complete article on use of SendKeys on codeproject
Here is how you simulate input (both mouse and keyboard).
http://msdn.microsoft.com/en-us/library/ms171548.aspx
If you look at the System.Windows.Forms.SendKeys class you will see that it provides you with what you want.
http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx

Categories

Resources