WPF keyboard shortcuts

Diego Alvarez 101 Reputation points

Are there any components or elements that allow you to configure keyboard shortcuts to click events of elements within WPF windows?

Thank you.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,710 questions
0 comments No comments
{count} votes

Accepted answer
  1. Sabrina Cosolo 126 Reputation points

    Hi Diego,
    I suggest you to use Google Translator to get my answer in Spanish, as I did to understand your question and translating it in my language.

    Here is some hints for your question, it's really succint so if you need more feel free to ask:

    //This is the class where you define a Command
    //In My case it is set in a folder named Commands inside the WPF project

    namespace MyCompany.MyApp.Commands
    public static class MyCommandsClass

        public static readonly RoutedCommand OpenConfig = new RoutedCommand();

    //This has to be set in the XAML part of the Window
    //In example: MyWindow.xaml

    ... More namespaces and class properties for Window
    ... >

    .... here you can have styles and other resources

    <!-- The following section of the window defines the event handlers for the command to be executed
    CanExecute and Executed are the command events and the 2 event handlers have to be set in your code
    to set if the command can execute and to implement the command code. -->
    Command="{x:Static lcmd:MyCommandsClass.CommandToExecute}"
    Executed="CommandToExecute_Executed" />

    <!-- The following section of the window assigns to the command a combination of keys used to execute the command.
    in my case, Alt+A executes the code put into rhe CommandToExecute_Executed event handler -->
    Command="{x:Static lcmd:MyCommandsClass.CommandToExecute}"
    Gesture="Alt+A" />

    //This part instead has to be written in the .xaml.cs part of the window
    // in example MyWindow.xaml.cs

    //Here is the event that WPF uses to activate the execution of the command
    //This is particularly useful to activate or deactivate buttons
    //In my case I check that the ViewModel of the window has been instantiated and that
    //a property has been instantiated
    private void CommandToExecute_CanExecute(object sender, CanExecuteRoutedEventArgs e)
    e.CanExecute = WModel != null && WModel.MyModelProperty != null;

    //Here is the event handler for the code executed when Alt+A is pressed
    //In this case I Get the CommandParameter value and pass it to the Method
    //That executes my code.
    private void CommandToExecute_Executed(object sender, ExecutedRoutedEventArgs e)
    string fieldName = e.Parameter.ToString();
    WModel.MyCommandFunction(this, fieldName);
    catch (Exception ex)

    HTH bye Sabrina

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Zaug 306 Reputation points

    hi, maybe make this
    create keydown event in window
    and write this
    if(e.Key==Key.LeftCtrl && Keyborad.IsKeyDown(Key.A))
    MessageBox.Show("Ctrl+a Pressed");

    0 comments No comments

  2. Diego Alvarez 101 Reputation points

    Thank you for your answers. :)

    0 comments No comments