multi-key InputGesture in WPF

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
Hi Dave,

This is a quick note to let you know that I am performing research on this
issue and will get back to you as soon as possible. I appreciate your
patience.



Regards,
Walter Wang ([email protected], 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.
 
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 ([email protected], 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.
 
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 ([email protected], 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.
 
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;
}
 
Back
Top