0

How to make customised hot keys in wpf application and perform various operations like making fields editable, save the window, go to a field index.

I am working on a wpf application that holds three windows + 1 main window. I am showing the hot keys shortcuts on the main window so those are visible on all the windows. Now I want to make the application perform the operation based on the keys I am hitting as hotkey combinations.

I have written the code but when I am running the application in debug mode the Window_KeyDown() method gets hit on first ever key hit. For example if I hit Alt+F then the code gets on Alt itself which gets into Keyboard.Modifiers .

  1. Code in mainwindow.xaml.cs

    private void Window_KeyDown(object sender, KeyEventArgs e) { e.Handled = true; var modifiers = Keyboard.Modifiers; var key = e.Key;

        if (key == Key.System)
        {
            key = e.SystemKey;
        }
    
        if (key == "C")
        {
            //make all the fields of the Window editable;
        }
        else if ((key == Key.D) && (modifiers == ModifierKeys.Alt))
        {
            //Perform delete operation
        } }
    
  2. Code in mainwindow.xaml

<Window x:Class="Namespace.MainWindow" Name="mainWindow"

    mc:Ignorable="d"
    Title="ApplicationName"
    Background="Black"
    Foreground="White"
    KeyDown="Window_KeyDown"
    d:DesignHeight="846.706"
    d:DesignWidth="857.544"
    WindowState="Maximized"
    >
  1. On window otherthan mainwindow

I am also trying to perform operation on single key hit. For example when I press 'C' my window fields' property= Isreadonly sets to false, by default I am setting it to true for textbox type field. This style property I am setting under each .XAML file for the three windows.

<Style TargetType="TextBox" x:Key="txtRow1Style"><Setter Property="FontFamily" Value="Segoe UI Semibold"></Setter><Setter Property="Margin" Value="5,0,0,0"></Setter><Setter Property="HorizontalAlignment" Value="Stretch"></Setter><Setter Property="Height" Value="20"></Setter><Setter Property="IsReadOnly" Value="True"></Setter><Style.Triggers><DataTrigger Binding="{Binding NameSpace.MainWindow.Key}" Value="C"><Setter Property="IsReadOnly" Value="False"></Setter></DataTrigger></Style.Triggers></Style>
coder9090
  • 63
  • 9

0 Answers0