How to add Hotkeys/Shortcuts in WPF

H

In any application, hotkeys (shortcuts) are useful for navigation as well as to launch a control’s code, unfortunatelly Hotkeys in WPF are not as easy as in Windows Forms.

In this post we will briefly present an example of how keyboard shortcuts can be used to execute commands using key bindings. What you will need is to create a new WPF window and follow the instructions below.

Step 1: In the mainWindow.xaml you have to put the following parts:

...
...
...
<Window.Resources>
    <ResourceDictionary>
        <RoutedUICommand x:Key="Ctr1" Text="Another Text" />
        <RoutedUICommand x:Key="Ctr2" Text="Another Text" />
        <RoutedUICommand x:Key="Ctr3" Text="Another Text" />
        <RoutedUICommand x:Key="Ctr4" Text="Another Text" />
        <RoutedUICommand x:Key="Ctr5" Text="Another Text" />
        <RoutedUICommand x:Key="Ctr6" Text="Another Text" />
        <RoutedUICommand x:Key="Ctr7" Text="Another Text" />
    </ResourceDictionary>
</Window.Resources>
...
...
...
<Window.InputBindings>
    <KeyBinding Key="F1" Modifiers="Ctrl" Command="{StaticResource Ctr1}" />
    <KeyBinding Key="F2" Modifiers="Ctrl" Command="{StaticResource Ctr2}" />
    <KeyBinding Key="F3" Modifiers="Ctrl" Command="{StaticResource Ctr3}" />
    <KeyBinding Key="F4" Modifiers="Ctrl" Command="{StaticResource Ctr4}" />
    <KeyBinding Key="F5" Modifiers="Ctrl" Command="{StaticResource Ctr5}" />
    <KeyBinding Key="F6" Modifiers="Ctrl" Command="{StaticResource Ctr6}" />
    <KeyBinding Key="F7" Modifiers="Ctrl" Command="{StaticResource Ctr7}" />
</Window.InputBindings>
...
...
...
<Window.CommandBindings>
    <CommandBinding Command="{StaticResource Ctr1}" Executed="CtrShortcut1" />
    <CommandBinding Command="{StaticResource Ctr2}" Executed="CtrShortcut2" />
    <CommandBinding Command="{StaticResource Ctr3}" Executed="CtrShortcut3" />
    <CommandBinding Command="{StaticResource Ctr4}" Executed="CtrShortcut4" />
    <CommandBinding Command="{StaticResource Ctr5}" Executed="CtrShortcut5" />
    <CommandBinding Command="{StaticResource Ctr6}" Executed="CtrShortcut6" />
    <CommandBinding Command="{StaticResource Ctr7}" Executed="CtrShortcut7" />
</Window.CommandBindings>
...
...
...

 

Step 2: In the mainWindow.cs you need to assign the corresponding methods

public void CtrShortcut1(Object sender, ExecutedRoutedEventArgs e)
{
    // Action
}

public void CtrShortcut2(Object sender, ExecutedRoutedEventArgs e)
{
    // Action
}

public void CtrShortcut3(Object sender, ExecutedRoutedEventArgs e)
{
    // Action
}

public void CtrShortcut4(Object sender, ExecutedRoutedEventArgs e)
{
    // Action
}

public void CtrShortcut5(Object sender, ExecutedRoutedEventArgs e)
{
    // Action
}

public void CtrShortcut6(Object sender, ExecutedRoutedEventArgs e)
{
    // Action
}

public void CtrShortcut7(Object sender, ExecutedRoutedEventArgs e)
{
    // Action
}

 

Disclaimer: The present content may not be used for training artificial intelligence or machine learning algorithms. All other uses, including search, entertainment, and commercial use, are permitted.

Leave a Reply to Pylouface Cancel reply

Categories

Tags