Multiple Key Gestures

SyntaxEditor for WPF Forum

Posted 13 years ago by Mick George - Senior Software Engineer, CNC Software, LLC
Version: 11.1.0545
Avatar
Hi,

I am trying to replicate Visual Studios shortcut key gestures for outlining, one example would be the following key gestures to toggle code outlining; CTRL+M followed by CTRL+L.

I have been unable, thus far, to find anything "out of the box" that would accomplish this functionality, is this functionality available and I missed it?

Thanks.

Comments (5)

Posted 13 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Mick,

Sorry, WPF doesn't include support "chord" key gestures and we don't have any WPF products at this time to do them.


Actipro Software Support

Posted 13 years ago by Mick George - Senior Software Engineer, CNC Software, LLC
Avatar
Thank you for the clarification.
Answer - Posted 8 years ago by Matt Whitfield
Avatar

Ok, so I'm digging up old threads again, but, based on http://stackoverflow.com/questions/2181991/multi-key-gesture-in-wpf you can do this:

class KeyChordGesture : KeyGesture
    {
        private readonly Key _key;
        private bool _gotFirstGesture;
        private readonly InputGesture _firstGesture;

        public KeyChordGesture(ModifierKeys modifier, Key firstKey, Key secondKey) : base(Key.None)
        {
            _firstGesture = new KeyGesture(firstKey, modifier);
            _key = secondKey;
        }

        public override bool Matches(object obj, InputEventArgs inputEventArgs)
        {
            var keyArgs = inputEventArgs as KeyEventArgs;
            if (keyArgs == null || keyArgs.IsRepeat)
            {
                return false;
            }

            if (_gotFirstGesture)
            {
                _gotFirstGesture = false;

                if (keyArgs.Key == _key)
                {
                    inputEventArgs.Handled = true;
                }

                return keyArgs.Key == _key;
            }
            
            _gotFirstGesture = _firstGesture.Matches(null, inputEventArgs);
            if (_gotFirstGesture)
            {
                inputEventArgs.Handled = true;
            }

            return false;
        }
    }

 Then associate it like this:

Editor.InputBindings.Add(new KeyBinding(EditorCommands.SelectAll, 
    new KeyChordGesture(ModifierKeys.Control, Key.K, Key.X)));

 Hope that helps anyone looking in the future.

Posted 3 years ago by Mick George - Senior Software Engineer, CNC Software, LLC
Avatar

Just revisited this post from many years ago because I ran into the same requirement and lo and behold Matt's solution works like a charm.

Thanks Matt!

Posted 2 years ago by Rick - Developer, Visual Software Systems LLC
Avatar

This is the solution that keeps on givin'.  Exactly what I needed.  Thanks Matt!

The latest build of this product (v24.1.2) was released 0 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.