WPF keyboard shortcuts

Diego Alvarez 101 Reputation points
2020-07-02T18:31:45.603+00:00

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
    2020-07-03T11:22:51.25+00:00

    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

    <Window
    x:Class="MYCompany.MyApp.Windows.MyWindow"
    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:lcmd="clr-namespace:MYCompany.MyApp.Commands"
    ... More namespaces and class properties for Window
    ... >

    <Window.Resources>
    .... here you can have styles and other resources
    </Window.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. -->
    <Window.CommandBindings>
    <CommandBinding
    CanExecute="CommandToExecute_CanExecute"
    Command="{x:Static lcmd:MyCommandsClass.CommandToExecute}"
    Executed="CommandToExecute_Executed" />
    </Window.CommandBindings>

    <!-- 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 -->
    <Window.InputBindings>
    <KeyBinding
    Command="{x:Static lcmd:MyCommandsClass.CommandToExecute}"
    CommandParameter="ParameterValue"
    Gesture="Alt+A" />
    <Window.InputBindings>

    //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)
    {
    try
    {
    string fieldName = e.Parameter.ToString();
    WModel.MyCommandFunction(this, fieldName);
    }
    catch (Exception ex)
    {
    EventLogger.SendMsg(ex);
    Msg.Error(ex);
    }
    }

    HTH bye Sabrina

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Zaug 306 Reputation points
    2021-08-25T09:16:16.047+00:00

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

    0 comments No comments

  2. Diego Alvarez 101 Reputation points
    2021-08-25T12:37:25.7+00:00

    Thank you for your answers. :)

    0 comments No comments