Creating KeyBinding in WPF with more than one modifier key

前端 未结 5 1437
南旧
南旧 2020-12-08 17:51

The way I created KeyBinding was something like:


         


        
5条回答
  •  时光取名叫无心
    2020-12-08 18:33

    Here is my code to implement multiple character shortcut keys, such as Alt + P + A in WPF MVVM.

    Add this to your XAML (attached behavior for the KeyDown event):

    cb:ShortCutBehavior.Command="{Binding Shortcuts.CmdKeyPressed}"
    

    Add this to your view model:

    ShortCuts Shortcuts = new ShortCuts( this );
    
    //Add Plenty of shortcuts here until your heart is desired
    
    Shortcuts.AddDoubleLetterShortCut( AddOrganization, Key.P, Key.A, ModifierKeys.Alt, true);
    Shortcuts.AddSingleLetterShortCut( CmdAddNewAgreement, Key.A, ModifierKeys.Alt);
    

    These are two examples of adding shortcuts. The first is a double letter shortcut: Alt + P + A which runs the method AddOrganization() and the second is a single letter shortcut: Alt + A which executes the ICommand CmdAddNewAgreemnt.

    Both AddDoubleLetterShortCut and AddSingleLetterShortCut are overloaded to accept Actions or ICommands.

    This is one of my first attempts at generisizing something, so you can take the idea and make it suitable for you.

提交回复
热议问题