WPF C# Detect CTRL + A (PreviewKeyDown)


XAML:
<Window x:Class="LassoSelection.Controls.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WPF C# Detect CTRL + A" 
        Height="350" 
        Width="525" 
        PreviewKeyDown="WindowKeyDown">
    <Grid>
    Grid>
Window>

Code Behind:
using System.Windows;
using System.Windows.Input;
 
public partial class MainWindow
{
    public MainWindow()
    {
        InitializeComponent();
    }
 
    private void WindowKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.A && Keyboard.Modifiers == ModifierKeys.Control)
        {
            MessageBox.Show("CTRL + A Pressed!");
        }
    }
}
 
References:

Comments

  1. Thanks for this.
    Quick and to the point.

    ReplyDelete
  2. Thanks a lot...helped me a lot!
    God Bless!
    -Prashant

    ReplyDelete

Post a Comment

Popular posts from this blog

WPF How to Dispose ViewModel when the associated UserControl (Not Window) closes?

C# How to unit test Dispatcher

WPF: How to Deep Copy WPF object (e.g. UIElement) ?