there is two ways to add wpf key short cut.
1 2 3 4 5 6 7 8 9 | this.KeyUp += MainWindow_KeyUp; private void MainWindow_KeyUp(object sender, KeyEventArgs e) { if(e.Key == Key.D1) { MessageBox.Show("1 is press"); } } |
but for the most normal way to do is to wpf resource.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <Window.Resources> <RoutedUICommand x:Key="ClickCommand" Text="Text" /> </Window.Resources> <Window.InputBindings> <KeyBinding Command="{Binding Path=button_Click}" Key="O" Modifiers="Control"/> <KeyBinding Key="C" Command="{StaticResource ClickCommand}" /> </Window.InputBindings> <Window.CommandBindings> <CommandBinding Command="{StaticResource ClickCommand}" Executed="button_Click" /> </Window.CommandBindings> <Grid> <Button x:Name="button1" Content="1" HorizontalAlignment="Left" Margin="101,140,0,0" VerticalAlignment="Top" Width="75" Click="button1_Click" Command="{StaticResource ClickCommand}"/> <Button x:Name="button" Content="2" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="181,140,0,0" Click="button_Click" Command="{StaticResource ClickCommand}"/> </Grid> |