none
WPF Keybinding in code behind RRS feed

  • Question

  • Hello, I would like to create a KeyBinding in code behind using this combo: Ctrl+Shift+Right+Left, so I did:

    KeyBinding b = new KeyBinding()
        {
            Command = SpvCommands.ChangeUser,
            Key = Key.LeftCtrl,
            //Gesture = new KeyGesture(Key.LeftShiftKey);
        };
        InputBindings.Add(b);

    How to create the gesture with multiple combination?
    Wednesday, December 19, 2018 10:41 AM

All replies

  • There are several properties:

     g.GestureKey = Key.X;
     g.GestureModifier = ModifierKeys.Control;
    
    

    https://docs.microsoft.com/en-us/dotnet/api/system.windows.input.keygesture?view=netframework-4.7.2

    If you take a look at ModifierKeys:

    https://docs.microsoft.com/en-us/dotnet/api/system.windows.input.modifierkeys?view=netframework-4.7.2

    It's actually a flags, so you can combine any of them:

    https://docs.microsoft.com/en-us/dotnet/api/system.flagsattribute?view=netframework-4.7.2

    g.GestureModifier = (ModifierKeys.Control | ModifierKeys.Shift );

    If you need multiple keys like leftctrl and rightctrl then you need two keybindings.

    You're binding a key press of a specific key, which can have modifiers such as ctrl / shift etc.


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Wednesday, December 19, 2018 11:36 AM
    Moderator