We all know we can assign a hot key to a button simply by prefixing the hot character in the content with an underscore. For example the XAML below executes the Underscore command if the user hits ALT+U (assuming the Underscore command can be executed)
<Button Content="_Underscore" Command="{StaticResource Underscore}"/>
While this is convenient it does have some limitations...
- The hot character must be in the content.
- It only works when the content is a string.
- It only works with the ALT modifier
- The hot character must be unique within the Window or User Control
If you want more control, you can use KeyBindings. The example below shows a binding that applies to the entire window. There is also a MouseBinding class.
<Window.InputBindings>
<KeyBinding Gesture="ALT+W" Command="{StaticResource WindowScope}"/>
</Window.InputBindings>
<KeyBinding Gesture="ALT+W" Command="{StaticResource WindowScope}"/>
</Window.InputBindings>
- Using the underscore technique
- On a control with window scope
- On a control with control scope
- Without any visible control
Start a new Visual Studio WPF Application project using .Net Framework and C#. Call it HotKeys.
MainWindow.XAML looks like this.
<Window x:Class="HotKeys.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:HotKeys"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance local:MainWindow}"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<RoutedCommand x:Key="Underscore"/>
<RoutedCommand x:Key="WindowScope"/>
<RoutedCommand x:Key="SearchTitle"/>
<RoutedCommand x:Key="SearchTerm"/>
<RoutedCommand x:Key="All"/>
</Window.Resources>
<Window.CommandBindings>
<CommandBinding Command="{StaticResource Underscore}" Executed="Underscore_Executed"/>
<CommandBinding Command="{StaticResource WindowScope}" Executed="Window_Executed"/>
<CommandBinding Command="{StaticResource SearchTitle}" Executed="SearchTitle_Executed" CanExecute="SearchTitle_CanExecute"/>
<CommandBinding Command="{StaticResource SearchTerm}" Executed="SearchTerm_Executed" CanExecute="SearchTerm_CanExecute"/>
<CommandBinding Command="{StaticResource All}" Executed="All_Executed"/>
</Window.CommandBindings>
<Window.InputBindings>
<KeyBinding Gesture="ALT+W" Command="{StaticResource WindowScope}"/>
<KeyBinding Gesture="CTRL+A" Command="{StaticResource All}"/>
</Window.InputBindings>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="10"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<Button Grid.Row="0" Grid.Column="0" Content="_Underscore" Command="{StaticResource Underscore}"/>
<Button Grid.Row="1" Grid.Column="0" Content="Window Scope" Command="{StaticResource WindowScope}"/>
<StackPanel Grid.Row="2" Grid.Column="0" Orientation="Vertical">
<TextBlock Text="Search Titles"/>
<TextBox Text="{Binding SearchTitle, UpdateSourceTrigger=PropertyChanged}">
<TextBox.InputBindings>
<KeyBinding Gesture="ALT+S" Command="{StaticResource SearchTitle}"/>
</TextBox.InputBindings>
</TextBox>
</StackPanel>
<StackPanel Grid.Row="2" Grid.Column="1" Orientation="Vertical">
<TextBlock Text="Search Terms"/>
<TextBox Text="{Binding SearchTerm, UpdateSourceTrigger=PropertyChanged}">
<TextBox.InputBindings>
<KeyBinding Gesture="ALT+S" Command="{StaticResource SearchTerm}"/>
</TextBox.InputBindings>
</TextBox>
</StackPanel>
<TextBlock Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" Text="Hit ALT+U, ALT+W, ALT+S, or CTRL+A"/>
</Grid>
</Window>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:HotKeys"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance local:MainWindow}"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<RoutedCommand x:Key="Underscore"/>
<RoutedCommand x:Key="WindowScope"/>
<RoutedCommand x:Key="SearchTitle"/>
<RoutedCommand x:Key="SearchTerm"/>
<RoutedCommand x:Key="All"/>
</Window.Resources>
<Window.CommandBindings>
<CommandBinding Command="{StaticResource Underscore}" Executed="Underscore_Executed"/>
<CommandBinding Command="{StaticResource WindowScope}" Executed="Window_Executed"/>
<CommandBinding Command="{StaticResource SearchTitle}" Executed="SearchTitle_Executed" CanExecute="SearchTitle_CanExecute"/>
<CommandBinding Command="{StaticResource SearchTerm}" Executed="SearchTerm_Executed" CanExecute="SearchTerm_CanExecute"/>
<CommandBinding Command="{StaticResource All}" Executed="All_Executed"/>
</Window.CommandBindings>
<Window.InputBindings>
<KeyBinding Gesture="ALT+W" Command="{StaticResource WindowScope}"/>
<KeyBinding Gesture="CTRL+A" Command="{StaticResource All}"/>
</Window.InputBindings>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="10"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<Button Grid.Row="0" Grid.Column="0" Content="_Underscore" Command="{StaticResource Underscore}"/>
<Button Grid.Row="1" Grid.Column="0" Content="Window Scope" Command="{StaticResource WindowScope}"/>
<StackPanel Grid.Row="2" Grid.Column="0" Orientation="Vertical">
<TextBlock Text="Search Titles"/>
<TextBox Text="{Binding SearchTitle, UpdateSourceTrigger=PropertyChanged}">
<TextBox.InputBindings>
<KeyBinding Gesture="ALT+S" Command="{StaticResource SearchTitle}"/>
</TextBox.InputBindings>
</TextBox>
</StackPanel>
<StackPanel Grid.Row="2" Grid.Column="1" Orientation="Vertical">
<TextBlock Text="Search Terms"/>
<TextBox Text="{Binding SearchTerm, UpdateSourceTrigger=PropertyChanged}">
<TextBox.InputBindings>
<KeyBinding Gesture="ALT+S" Command="{StaticResource SearchTerm}"/>
</TextBox.InputBindings>
</TextBox>
</StackPanel>
<TextBlock Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" Text="Hit ALT+U, ALT+W, ALT+S, or CTRL+A"/>
</Grid>
</Window>
public partial class MainWindow : Window
public string SearchTitle { get; set; }
InitializeComponent();
}
MessageBox.Show("You underscored");
MessageBox.Show("You windowed");
MessageBox.Show($"You searched for title '{SearchTitle}'");
MessageBox.Show($"You searched for term '{SearchTerm}'");
e.CanExecute = !string.IsNullOrEmpty(SearchTitle);
e.CanExecute = !string.IsNullOrEmpty(SearchTerm);
MessageBox.Show("You hit CTRL+A");
}
}
No comments:
Post a Comment