Home All Groups Group Topic Archive Search About

multi-key InputGesture in WPF

Author
11 Oct 2007 10:22 PM
Dave Weeden
Hi,

Is it possible to create an InputGesture that is a multi-keystroke
combination.

e.g. like the Ctrl+E,C shortcut for the 'Comment Selection' command under
Visual Studio's Edit->Advanced menu

My first try was to OR the values together as follows:

InsertGroupCommand.InputGestures.Add(new KeyGesture(Key.I | Key.G,
ModifierKeys.Control));

That compiled ok but didn't work properly.  I've also tried created adding
two gestures to the collection (one for 'I' and one for 'G').  No luck.

Any ideas?

Thanks,

Dave

Author
12 Oct 2007 10:25 AM
Walter Wang [MSFT]
Hi Dave,

WPF currently doesn't have built-in support for such key chains to do that.
InputGesture or KeyGesture is not going to help here.

I suggest you to handle the KeyUp event and use a member variable to do
that. For example, here's some code to handle Ctrl+K, Ctrl+C:

private bool kPressed = false;

private void KeyUpEventHanlder(object sender, KeyEventArgs e)
{
// Ctrl + C
if ((Keyboard.Modifiers == ModifierKeys.Control) && (e.Key == Key.C))
{
if (this.kPressed)
MessageBox.Show("You pressed Ctrl+K followed by Ctrl+C!");
}

                                this.kPressed = false;

// Ctrl + K
if ((Keyboard.Modifiers == ModifierKeys.Control) && (e.Key == Key.K))
{
this.kPressed = true;
}
}


Hope this helps.


Regards,
Walter Wang (waw***@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Author
17 Oct 2007 1:58 AM
Walter Wang [MSFT]
Hi Dave,

I'm writing to check the status of this post. Please feel free to let me
know if there's anything else I can help. Thanks.


Regards,
Walter Wang (waw***@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Author
31 Oct 2007 10:34 AM
Dave Weeden
Hi Walter,

I've used a different wrinkle of your idea and everything is working
properly now. 

The code I used is below for anyone interested.

I would have liked to have used my CanExecute handlers directly but it
appears there is no constructor for CanExecuteRoutedEventArgs so I had to
make a helper method which could be called by both the actual CanExecute
handler as well as my command simulation code below.

Thanks,

Dave

private KeyEventArgs lastKeyUpArgs = null;

protected override void OnKeyUp(KeyEventArgs e)
{
  // call base handler
  base.OnKeyUp(e);

  // process event
  if (Keyboard.Modifiers == ModifierKeys.Control)
  {
    switch (e.Key)
    {
      case Key.G:

        if (lastKeyUpArgs != null)
        {
          if (lastKeyUpArgs.Key == Key.A)
          {
            // add group
            if (AddGroup_CanExecute_Helper())
            {
              AddGroup_Executed(this, null);
            }
          }
          else if (lastKeyUpArgs.Key == Key.I)
          {
            // insert group
            if (InsertGroup_CanExecute_Helper())
            {
              InsertGroup_Executed(this, null);
            }
          }
        }
        break;

      case Key.P:

        if (lastKeyUpArgs != null)
        {
          if (lastKeyUpArgs.Key == Key.A)
          {
            // add parameter
            if (AddParameter_CanExecute_Helper())
            {
              AddParameter_Executed(this, null);
            }
          }
          else if (lastKeyUpArgs.Key == Key.I)
          {
            // insert parameter
            if (InsertParameter_CanExecute_Helper())
            {
              InsertParameter_Executed(this, null);
            }
          }
        }
        break;
    }
  }

  // save argument for next event
  lastKeyUpArgs = e;
}

""Walter Wang [MSFT]"" wrote:

Show quote
> Hi Dave,
>
> WPF currently doesn't have built-in support for such key chains to do that.
> InputGesture or KeyGesture is not going to help here.
>
> I suggest you to handle the KeyUp event and use a member variable to do
> that. For example, here's some code to handle Ctrl+K, Ctrl+C:
>
> private bool kPressed = false;
>
> private void KeyUpEventHanlder(object sender, KeyEventArgs e)
> {
> // Ctrl + C
> if ((Keyboard.Modifiers == ModifierKeys.Control) && (e.Key == Key.C))
> {
> if (this.kPressed)
> MessageBox.Show("You pressed Ctrl+K followed by Ctrl+C!");
> }
>                                
>                                 this.kPressed = false;
>
> // Ctrl + K
> if ((Keyboard.Modifiers == ModifierKeys.Control) && (e.Key == Key.K))
> {
> this.kPressed = true;
> }
> }
>
>
> Hope this helps.
>
>
> Regards,
> Walter Wang (waw***@online.microsoft.com, remove 'online.')
> Microsoft Online Community Support
>
> ==================================================
> When responding to posts, please "Reply to Group" via your newsreader so
> that others may learn and benefit from your issue.
> ==================================================
>
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>

AddThis Social Bookmark Button